在函数调用上将GTK子窗口小部件添加到主窗口

时间:2014-07-18 07:43:12

标签: c++ c widget gtk

我正在尝试在GtkNotebook窗口小部件中添加GtkWindow窗口小部件。窗口小部件与GtkBox一起在ui文件中创建。

myclass:: myclass()
{       
GtkBuilder *builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "uifile.ui", NULL);
gtk_builder_connect_signals (builder, NULL);
mainWindow = GTK_WIDGET (gtk_builder_get_object(builder, "mainWindow"));
box = GTK_BOX (gtk_builder_get_object(builder, "box1"));
gtk_widget_show_all (mainWindow);
}

我在myclass中有这个函数void newTabview定义为GtkWidget *view

void myclass:: newTab()
{
view = gtk_notebook_new();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (view), GTK_POS_TOP);
gtk_box_pack_start(GTK_BOX (box),view, TRUE, TRUE, 0);
gtk_box_reorder_child (GTK_BOX (box), view, 1);
gtk_widget_show (view);
}

但是当我调用此函数时,我得到segmentation fault。 这不是将小部件添加到mainWindow的正确方法吗? 我该如何纠正?

有关myclass.h和myclass.cpp文件的完整代码,请参阅此处http://pastebin.com/tWTUKtZG

1 个答案:

答案 0 :(得分:0)

这里我在python3,gtk3中进行,基本相同

class myclass ():
   def __init__ (self):   
      mainWindow = # construct from builder
      tab = self.newTab ()
      mainWindow.add (tab)

      mainWindow.show_all ()


   def newTab (self):
      view = Gtk.Notebook ()
      view.show ()
      return view

   # in C
   # GtkWidget* newTab ()
   # {
   #    view = Gtk.Notebook ()
   #    view.show ()
   #    return view
   # } 

也许你在调用void onTab()之前将GtkWidget *视图添加到mainWindow中,如果你这样做了,那就相反了