我正在尝试使用glade和gtk在C语言中创建一个简单的GUI。在GUI中,我想添加一个绘制数据的小部件(只需要一个折线图和一个直方图)(不断更新),但我不知道该用什么。我看到gnuplot很受欢迎,但我不知道如何在应用程序中使用它。我希望能得到一些建议和说明,因为我找不到相关资料。
这是一些示例代码,我将其用作基础,只是为了让您有个想法。
更新: 我在我的电脑上构建了gnuplot,现在当我运行代码时它不会打开一个新窗口,似乎在我的窗口内绘制。唯一的问题是我的gtkbox容器中的小部件不会调整大小,所以它看起来像是一个小白条。我认为它是正常工作的,因为我已经去掉了……
我手动测试了代码,得到了与此处发现的相同的结果,因此我将尝试构建gnuplot并更改配置文件。是否有该帖子中解决方案的替代方法?
这是一些示例代码,我将其用作基础,只是为了让您有个想法。
更新: 我在我的电脑上构建了gnuplot,现在当我运行代码时它不会打开一个新窗口,似乎在我的窗口内绘制。唯一的问题是我的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