GTK Glade3显示文本
#include <gtk-2.0/gtk/gtk.h> #include <stdlib.h> gint delete_event( GtkWidget *widget,GdkEvent *event,gpointer data ) { gtk_main_quit (); return FALSE; } void openfile(GtkWidget *widget, gpointer *data) { // GtkWidget *FileSelection; // FileSelection = gtk_file_selection_new ("选择文件"); //创建文件选择构件 // gtk_file_selection_set_filename (GTK_FILE_SELECTION (FileSelection),"*.txt *.doc"); // gtk_widget_show (FileSelection); GtkWidget *dialog; // FILE *file; dialog = gtk_file_chooser_dialog_new("Open File",NULL,GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL); if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { gchar *filename; gchar *content; gsize bytes; GError *error = NULL; GtkTextBuffer *buffer; filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); // open_file(filename); // file=fopen(filename,"rb"); g_file_get_contents(filename, &content, &bytes, &error); if (error != NULL) { g_printf(error->message); g_clear_error(&error); } buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (text)); gtk_text_buffer_set_text (buffer, content, -1); g_free(filename); } gtk_widget_destroy(dialog); } int main(int argc,char* argv[]) { /*创建指向GtkWidget类型的指针*/ GtkWidget *window; GtkBuilder *builder = NULL; /*初始化*/ gtk_init(&argc,&argv); builder=gtk_builder_new(); gtk_builder_add_from_file(builder,"text.glade",NULL); gtk_builder_connect_signals(builder, NULL); // 根据 ID 获取子构件 window=GTK_WIDGET(gtk_builder_get_object(builder,"window")); // g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(PressClear),NULL); g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (gtk_main_quit), NULL); // g_signal_connect (G_OBJECT(menubar), "activate",G_CALLBACK (openfile), NULL); g_object_unref(G_OBJECT(builder)); gtk_widget_show_all(window); gtk_main(); return 0; }
下面是glade3的xml
<?xml version="1.0" encoding="UTF-8"?> <interface> <requires lib="gtk+" version="2.16"/> <!-- interface-naming-policy project-wide --> <object class="GtkWindow" id="window"> <child> <object class="GtkVBox" id="vbox"> <property name="visible">True</property> <child> <object class="GtkMenuBar" id="menubar"> <property name="visible">True</property> <property name="ubuntu_local">True</property> <child> <object class="GtkMenuItem" id="file"> <property name="visible">True</property> <property name="label" translatable="yes">文件(_F)</property> <property name="use_underline">True</property> <child type="submenu"> <object class="GtkMenu" id="menu1"> <property name="visible">True</property> <property name="ubuntu_local">True</property> <child> <object class="GtkImageMenuItem" id="new"> <property name="label">gtk-new</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="open"> <property name="label">gtk-open</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> <signal name="activate" handler="openfile" object="text"/> </object> </child> <child> <object class="GtkImageMenuItem" id="save"> <property name="label">gtk-save</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="save-as"> <property name="label">gtk-save-as</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkSeparatorMenuItem" id="separatormenuitem1"> <property name="visible">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="quit"> <property name="label">gtk-quit</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> </object> </child> </object> </child> <child> <object class="GtkMenuItem" id="edit"> <property name="visible">True</property> <property name="label" translatable="yes">编辑(_E)</property> <property name="use_underline">True</property> <child type="submenu"> <object class="GtkMenu" id="menu2"> <property name="visible">True</property> <property name="ubuntu_local">True</property> <child> <object class="GtkImageMenuItem" id="cut"> <property name="label">gtk-cut</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="copy"> <property name="label">gtk-copy</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="paste"> <property name="label">gtk-paste</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> <child> <object class="GtkImageMenuItem" id="delete"> <property name="label">gtk-delete</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> </object> </child> </object> </child> <child> <object class="GtkMenuItem" id="view"> <property name="visible">True</property> <property name="label" translatable="yes">视图(_V)</property> <property name="use_underline">True</property> </object> </child> <child> <object class="GtkMenuItem" id="help"> <property name="visible">True</property> <property name="label" translatable="yes">帮助(_H)</property> <property name="use_underline">True</property> <child type="submenu"> <object class="GtkMenu" id="menu3"> <property name="visible">True</property> <property name="ubuntu_local">True</property> <child> <object class="GtkImageMenuItem" id="about"> <property name="label">gtk-about</property> <property name="visible">True</property> <property name="use_underline">True</property> <property name="use_stock">True</property> </object> </child> </object> </child> </object> </child> </object> <packing> <property name="expand">False</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkTextView" id="text"> <property name="width_request">700</property> <property name="height_request">500</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="pixels_above_lines">1</property> <property name="pixels_below_lines">1</property> <property name="wrap_mode">word-char</property> <property name="left_margin">1</property> <property name="right_margin">1</property> </object> <packing> <property name="position">1</property> </packing> </child> <child> <object class="GtkStatusbar" id="statusbar"> <property name="visible">True</property> <property name="tooltip_text" translatable="yes">状态栏</property> <property name="spacing">2</property> </object> <packing> <property name="expand">False</property> <property name="position">2</property> </packing> </child> </object> </child> </object> </interface>
我想做的是打开一个文件并显示在文本中(text构件)、
菜鸟求解释
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
能帮忙解释下么,小弟最近有兴趣就看了下GTK ,想求一个完整到记事本源码参考。
如果在XML里设置了user data,GtkBuilder就会交换参数的顺序。
将C代码中第33行改为
即可。
我记得gtk+开发环境有一个doc和demo的程序。。你可以看看
坐等、