2016-12-16 69 views
0

我正在使用codeblocks和glade編寫一個c程序。起初,我認爲這個問題是因爲我的代碼。我嘗試了我在網上找到的示例代碼,它不使用glade - 僅使用GTK - 而且它工作正常。然後,我嘗試了一個只有一個窗口和一個textview的新世界,但它並沒有工作。Glade Textview不顯示文字

GtkTextBuffer *buffer= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview1)); 
gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0); 
gtk_text_buffer_insert(buffer, &iter, "Plain text ... it's working\n", -1); 

//gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1); 
gtk_widget_show_all(window); 
gtk_main(); 

我也嘗試了註釋行。所以問題是如何讓glade textviews顯示文本。

值得一提的是,我使用glade本身的硬編​​碼文本沒有出現。 這裏是完整的代碼。

GtkWidget *textview1; 

int main(int argc, char *argv[]) 
{ 
GtkBuilder  *builder; 
GtkWidget  *window; 
gtk_init(&argc, &argv); 
GtkTextIter iter; 

builder = gtk_builder_new(); 
gtk_builder_add_from_file (builder, "Unsaved 1.glade", NULL); 

window = GTK_WIDGET(gtk_builder_get_object(builder, "window1")); 
gtk_builder_connect_signals(builder, NULL); 

g_object_unref(builder); 
textview1=GTK_WIDGET(gtk_builder_get_object(builder, "textview1")); 



GtkTextBuffer *buffer= gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview1)); 
gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0); 
gtk_text_buffer_insert(buffer, &iter, "Plain text ... it's working\n", -1); 
gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1); 
gtk_widget_show_all(window); 
gtk_main(); 
struct Student_t* arrayOfStudents=load(); 
return 0; 
} 

空地文件:

<interface> 
<requires lib="gtk+" version="2.16"/> 
<!-- interface-naming-policy project-wide --> 
<object class="GtkWindow" id="window1"> 
<property name="window_position">center</property> 
<property name="default_width">1000</property> 
<property name="default_height">600</property> 
<child> 
<object class="GtkScrolledWindow" id="scrolledwindow1"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="hscrollbar_policy">automatic</property> 
<property name="vscrollbar_policy">automatic</property> 
<child> 
<object class="GtkVBox" id="vbox2"> 
<property name="visible">True</property> 
<property name="orientation">vertical</property> 
<child> 
<object class="GtkHBox" id="hbox2"> 
<property name="visible">True</property> 
<child> 
<object class="GtkButton" id="button1"> 
<property name="label" translatable="yes">gtk-add</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
<property name="image_position">top</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button2"> 
<property name="label" translatable="yes">gtk-refresh</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button3"> 
<property name="label" translatable="yes">gtk-find</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">2</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button4"> 
<property name="label" translatable="yes">Statistics</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_underline">True</property> 
</object> 
<packing> 
<property name="position">3</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button5"> 
<property name="label" translatable="yes">Normalize</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="xalign">0.44999998807907104</property> 
<property name="yalign">0.44999998807907104</property> 
</object> 
<packing> 
<property name="position">4</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button10"> 
<property name="label" translatable="yes">gtk-edit</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">5</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox3"> 
<property name="visible">True</property> 
<child> 
<object class="GtkButton" id="button6"> 
<property name="label" translatable="yes">gtk-open</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button7"> 
<property name="label" translatable="yes">gtk-save</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button8"> 
<property name="label" translatable="yes">gtk-delete</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">2</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button11"> 
<property name="label" translatable="yes">Raise HL</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
</object> 
<packing> 
<property name="position">3</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button9"> 
<property name="label" translatable="yes">gtk-quit</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="position">4</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="position">1</property> 
</packing> 
</child> 
<child> 
<object class="GtkTextView" id="textview1"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="buffer">textbuffer1</property> 
</object> 
<packing> 
<property name="position">2</property> 
</packing> 
</child> 
<child> 
<object class="GtkStatusbar" id="statusbar1"> 
<property name="visible">True</property> 
<property name="spacing">2</property> 
<child> 
<object class="GtkLabel" id="label1"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">Mazen Sharkawy</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkLabel" id="label2"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">Shorouk Assem</property> 
</object> 
<packing> 
<property name="position">2</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="position">3</property> 
</packing> 
</child> 
</object> 
</child> 
</object> 
</child> 
</object> 
<object class="GtkDialog" id="dialog1"> 
<property name="border_width">5</property> 
<property name="type_hint">normal</property> 
<property name="has_separator">False</property> 
<child internal-child="vbox"> 
<object class="GtkVBox" id="dialog-vbox1"> 
<property name="visible">True</property> 
<property name="orientation">vertical</property> 
<property name="spacing">2</property> 
<child> 
<object class="GtkVBox" id="vbox1"> 
<property name="visible">True</property> 
<property name="orientation">vertical</property> 
<child> 
<object class="GtkHBox" id="hbox1"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label1"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">ID</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry1"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox2"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label2"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">Name</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry2"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox3"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label3"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">GPA</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry3"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">2</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox4"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label4"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">E-mail</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry4"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">3</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox6"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label5"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">Mobile</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry5"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">4</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox7"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label6"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">Age</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry6"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">5</property> 
</packing> 
</child> 
<child> 
<object class="GtkHBox" id="hbox8"> 
<property name="visible">True</property> 
<child> 
<object class="GtkLabel" id="label7"> 
<property name="visible">True</property> 
<property name="label" translatable="yes">City</property> 
</object> 
<packing> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkEntry" id="entry7"> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="invisible_char">●</property> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">6</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="position">1</property> 
</packing> 
</child> 
<child internal-child="action_area"> 
<object class="GtkHButtonBox" id="dialog-action_area1"> 
<property name="visible">True</property> 
<property name="layout_style">end</property> 
<child> 
<object class="GtkButton" id="button2"> 
<property name="label" translatable="yes">gtk-cancel</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="fill">False</property> 
<property name="position">0</property> 
</packing> 
</child> 
<child> 
<object class="GtkButton" id="button1"> 
<property name="label" translatable="yes">gtk-save</property> 
<property name="visible">True</property> 
<property name="can_focus">True</property> 
<property name="receives_default">True</property> 
<property name="use_stock">True</property> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="fill">False</property> 
<property name="position">1</property> 
</packing> 
</child> 
</object> 
<packing> 
<property name="expand">False</property> 
<property name="pack_type">end</property> 
<property name="position">0</property> 
</packing> 
</child> 
</object> 
</child> 
<action-widgets> 
<action-widget response="0">button2</action-widget> 
<action-widget response="0">button1</action-widget> 
</action-widgets> 
</object> 
<object class="GtkTextBuffer" id="textbuffer1"> 
<property name="text" translatable="yes">shit happens</property> 
</object> 
</interface> 
+0

請顯示更多代碼。您發佈的代碼段沒有任何問題;該錯誤在別處。 – andlabs

+0

@andlabs我添加了整個代碼。 –

回答

0

當你調用g_object_unref(builder);,你說你有沒有使用builder了意向。它類似於說free(builder),除了它使用GObject的引用計數,這是更復雜,並超出了這個答案的範圍。重點是:您在撥打g_object_unref()後仍然試圖使用builder,並且失敗。在你的情況下,它確實如此,所以textview1變量永遠不會引用你的文本視圖,所以GtkTextBuffer操作不起作用。從你的代碼中刪除g_object_unref()行,並且一切都應該工作。

+0

我確實刪除了它,但問題仍然存在。沒有改變。 –

+0

在這種情況下,請同時顯示您的glade文件。 – andlabs

+0

添加了沼地文件 –