如何在 gtk 和 glade 中嵌入情节

how to embed plot in gtk and glade

我正在尝试使用 C 中的 glade 和 gtk 创建一个简单的图形用户界面。在图形用户界面中,我想添加一个绘制数据的小部件(我想要的是线图和直方图)(不断更新)和我不知道该用什么。我看到 gnuplot 很流行,但我不知道如何在应用程序中使用它。我想要一些建议和一些说明,因为我找不到它。

这是我用作基础的一些示例代码,仅供您参考

更新: 我在我的电脑上构建了 gnuplot,现在当我 运行 代码时它不会打开一个新的 window 并且似乎在我的 window 中绘制。唯一的问题是我的 gtkbox 容器中的小部件没有调整大小,所以它看起来像一个小白条。我假设它正在工作,因为我删除了

gui.graph = gnuplot_init() ;
    gnuplot_setstyle(gui.graph, "lines") ;
    gnuplot_cmd(gui.graph, "set terminal x11 window \"%x\"", (int)gtk_socket_get_id (gui.socket));
    gnuplot_plot_slope(gui.graph, 1.0, 0.0, "unity slope") ;

和 运行 单独终端上的命令和我提到的白色栏弹出。

#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include "variables.h"
#include "gnuplot_i.h"

struct test tv;
struct GUI gui;

// Functions
void GUISetup();

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    gui.builder = gtk_builder_new_from_file("glade/window_main.glade");

    gui.window = GTK_WIDGET(gtk_builder_get_object(gui.builder, "window_main"));
    gtk_builder_connect_signals(gui.builder, NULL);
    
    GUISetup();
    
    g_object_unref(gui.builder);
    // start fullbuilderscreen
    gtk_window_fullscreen(GTK_WINDOW(gui.window));
    gtk_widget_show(gui.window);                
    gtk_main();

    return 0;
}

void GUISetup(){
    gui.socket = gtk_socket_new ();
    gtk_container_add (GTK_CONTAINER (gui.MainContainer), gui.socket);
    //gtk_widget_show (gui.socket);
    //gtk_widget_realize (gui.socket);
    
    gui.graph = gnuplot_init() ;
    gnuplot_setstyle(gui.graph, "lines") ;
    gnuplot_cmd(gui.graph, "set terminal x11 window \"%x\"", (int)gtk_socket_get_id (gui.socket));
    gnuplot_plot_slope(gui.graph, 1.0, 0.0, "unity slope") ;
}

当我做的时候我得到了这个:

gcc -c -g -O0 -Wall -pthread -pipe src/main.c `pkg-config --cflags --libs gtk+-3.0` -o main.o 
src/main.c: In function ‘GUISetup’:
src/main.c:66:86: warning: passing argument 1 of ‘gtk_socket_get_id’ from incompatible pointer type [-Wincompatible-pointer-types]
 ui.graph, "set terminal x11 window \"%x\"", (int)gtk_socket_get_id (gui.socket));
                                                                     ^~~
In file included from /usr/include/gtk-3.0/gtk/gtkx.h:29:0,
                 from src/main.c:25:
/usr/include/gtk-3.0/gtk/gtksocket.h:81:12: note: expected ‘GtkSocket * {aka struct _GtkSocket *}’ but argument is of type ‘GtkWidget * {aka struct _GtkWidget *}’
 Window     gtk_socket_get_id          (GtkSocket *socket_);
            ^~~~~~~~~~~~~~~~~
gcc -o interface main.o gnuplot_i.o -pthread `pkg-config --cflags --libs gtk+-3.0` -export-dynamic

我手动测试了代码并得到了与 here 相同的结果,所以我将尝试构建 gnuplot 并更改配置文件。在 post?

中找到的解决方案是否有任何替代方案?

对于 gnuplot:

http://ndevilla.free.fr/gnuplot/gnuplot_i/index.html

或者您可以使用 Gtk::Socket,首先创建一个 Gtk::Socket 并将其添加到您的小部件。然后将套接字 ID(十六进制)传递给 gnuplot 'set 终端 x11 window“ID”'。就是这样!

Matplot 可以更好:

https://github.com/lava/matplotlib-cpp