2012-02-06 31 views
4

我试图创建一个使用GtkBuilder和格莱德一个真的简单的GUI。为了实现这一点,我遵循官方的Gtk + 3参考手册的教程。原码唯一的区别是,我不连接到widget的信号,简单(因此除去过多的回调函数):GtkBuilder忽略的.ui文件

#include <gtk/gtk.h> 

int 
main (int argc, 
     char *argv[]) 
{ 
    GtkBuilder *builder; 
    GObject *window; 
    GObject *button; 

    gtk_init (&argc, &argv); 

    /* Construct a GtkBuilder instance and load our UI description */ 
    builder = gtk_builder_new(); 
    gtk_builder_add_from_file (builder, "builder.ui", NULL); 


    gtk_main(); 

    return 0; 
} 

在教程中使用的“builder.ui”文件的样子这个:

 <interface> 
    <object id="window" class="GtkWindow"> 
    <property name="visible">True</property> 
    <property name="title">Grid</property> 
    <property name="border-width">10</property> 
    <child> 
     <object id="grid" class="GtkGrid"> 
     <property name="visible">True</property> 
     <child> 
      <object id="button1" class="GtkButton"> 
      <property name="visible">True</property> 
      <property name="label">Button 1</property> 
      </object> 
      <packing> 
      <property name="left-attach">0</property> 
      <property name="top-attach">0</property> 
      </packing> 
     </child> 
     <child> 
      <object id="button2" class="GtkButton"> 
      <property name="visible">True</property> 
      <property name="label">Button 2</property> 
      </object> 
      <packing> 
      <property name="left-attach">1</property> 
      <property name="top-attach">0</property> 
      </packing> 
     </child> 
     <child> 
      <object id="quit" class="GtkButton"> 
      <property name="visible">True</property> 
      <property name="label">Quit</property> 
      </object> 
      <packing> 
      <property name="left-attach">0</property> 
      <property name="top-attach">1</property> 
      <property name="width">2</property> 
      </packing> 
     </child> 
     </object> 
     <packing> 
     </packing> 
    </child> 
    </object> 
</interface> 

......并且根本没有问题。该程序编译并生成所需的窗口。但是,当我尝试使用我自己的.ui文件(由glade 3.10.0生成)时,我根本没有得到任何结果。应用程序进入主循环并且不出现窗口。我使用的GUI文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<interface> 
    <!-- interface-requires gtk+ 3.0 --> 
    <object class="GtkWindow" id="Window"> 
    <property name="can_focus">False</property> 
    <property name="border_width">15</property> 
    <child> 
     <object class="GtkLabel" id="fooLabel"> 
     <property name="visible">True</property> 
     <property name="can_focus">False</property> 
     <property name="label" translatable="yes">foobar</property> 
     </object> 
    </child> 
    </object> 
</interface> 

我在做什么错?

回答

5

你只是读取XML文件,你需要一些代码,操纵用户界面的东西,例如显示顶层窗口在您的.ui:

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

    gtk_init (&argc, &argv); 

    builder = gtk_builder_new(); 
    gtk_builder_add_from_file (builder, "tutorial.xml", NULL); 
    window = GTK_WIDGET (gtk_builder_get_object (builder, "Window")); 
    g_object_unref (G_OBJECT (builder)); 
    gtk_widget_show (window);     
    gtk_main(); 

    return 0; 
} 
4

您的UI文件和教程UI文件之间的区别是这条线,在小部件的GtkWindow:

<property name="visible">True</property> 

没有它,你的窗口(和它里面的所有东西)都保持隐藏,所以这就是为什么“没有”发生。窗户在那里,它只是看不见。该行

gtk_widget_show (window); 

在nos的解决方案也解决了这个问题,但这就是为什么教程文件工作,而你的没有。

0

我有同样的问题,但尝试了上述所有的,但没有成功。

我习惯了在Windows C++,但这是在raspbian的树莓派一个G ++项目。

我唯一的解决办法,到目前为止是如下

gtk_builder_add_from_file (builder, "/home/pi/Documents/tutorial.xml", NULL); 

我随后包括编译器的路径,但随后再次没有窗口中添加的完整路径。

0

我有同样的问题。在Ubuntu linux上显示ok的简单应用程序并未出现在Raspbian中。经过一些调试之后,看起来像Raspbian的GTKBuilder实现有一个bug。这是行不通的。

解决方案: 1)无论是手动转换GUI描述XML到C/C++ API(GTK +)调用。 OR 2)编写一个小程序将GUI XML文件转换为C/C++ API(GTK +)调用。

谢谢。