[Glade-users] Loading widgets from XML file using libglade

dhk dhkuhl at optonline.net
Wed Dec 16 18:37:28 EST 2009


Andrew Wood wrote:
> Ah.  The rightpanevbox is inside the right portion of an HPane. I want 
> the left side of the HPane to stay the same (declared in code) but the 
> contents of the right side to change (and be loaded from Glade).
> 
> 
> The page1.glade file is as follows. I dont actually want that 'window1' 
> but I cant seem to find a way of setting the top level as just a vbox.
> 
> <?xml version="1.0"?>
> <glade-interface>
>  <!-- interface-requires gtk+ 2.16 -->
>  <!-- interface-naming-policy project-wide -->
>  <widget class="GtkWindow" id="window1">
>    <child>
>      <widget class="GtkVBox" id="mainvbox">
>        <property name="visible">True</property>
>        <property name="orientation">vertical</property>
>        <child>
>          <widget class="GtkLabel" id="label1">
>            <property name="visible">True</property>
>            <property name="label" translatable="yes">PAGE1</property>
>          </widget>
>          <packing>
>            <property name="position">0</property>
>          </packing>
>        </child>
>      </widget>
>    </child>
>  </widget>
> </glade-interface>
> 
> 
> 
> David López Luengo wrote:
>> The problem comes because your rightpanevbox widget is already inside 
>> another widget. Are you sure that in the mainvbox file definition, 
>> rightpanevbox is the top widget? If you post the xml would help a lot. 
>> It seems to be an easy-to-solve problem :-)
>>
>> Good luck!
>>
>>
>> -- 
>> David
>>
>>
>> 2009/12/16 Andrew Wood <ajwood at theiet.org <mailto:ajwood at theiet.org>>
>>
>>     Im having problems getting libglade to load widgets from an XML file.
>>     My aim is to have a GtkVBox in a window - the window is made in
>>     code not
>>     Glade, and the contents of the vbox will be loaded from Glade
>>     files such
>>     that the contents of the vbox can change as the app runs.
>>
>>     To test it out I created 2 Glade XML files page1.glade and 
>> page2.glade
>>     each have the following hierarchy:  GtkWindow->GtkVBox->GtkLabel
>>
>>     The label contains the text "Page1" or "Page2" accordingly. I want to
>>     load the GtkVBox from either file and use it to 'replace' a manually
>>     created vbox which is in code.
>>
>>     In the code which is just a short 'main' file Ive declared the
>>     following:
>>
>>     GladeXML* page1xml;
>>     GladeXML* page2xml;
>>
>>     GtkWidget* mainwindow;
>>     GtkWidget* rightpanescrollview;
>>     GtkWidget* rightpanevbox;
>>
>>     page1xml=glade_xml_new("./page1.glade",NULL,NULL);
>>     rightpanevbox= glade_xml_get_widget(page1xml,"mainvbox");
>>
>>     
>> gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(rightpanescrollview),rightpanevbox); 
>>
>>
>>     The rightpanevbox is then put into the window.
>>
>>     But when the app runs the label from Glade doesnt appear and the
>>     following is printed on the console:
>>
>>     (a.out:3894): Gtk-CRITICAL **: gtk_scrolled_window_add_with_viewport:
>>     assertion `child->parent == NULL' failed
>>     I tried decalring in code a GtkWindow and loading the top-level 
>> window
>>     widget from the XML file but again I got an error similar but 
>> slightly
>>     different suggesting that no widgets are actually being found in
>>     the file.
>>
>>     The file itself is found because it throws up an error if I change 
>> the
>>     filename in the call to  glade_xml_new but it seemingly cant find
>>     anything in that file.
>>
>>     Any suggestions? I can post the entire main code file if it would 
>> help
>>     its not very long.
>>
>>     Thanks
>>     Andrew
>>
>>     _______________________________________________
>>     Glade-users maillist  -  Glade-users at lists.ximian.com
>>     <mailto:Glade-users at lists.ximian.com>
>>     http://lists.ximian.com/mailman/listinfo/glade-users
>>
>>
>>
>>
>> -- 
>> David
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
> 
> 
> David López Luengo wrote:
>> The problem comes because your rightpanevbox widget is already inside 
>> another widget. Are you sure that in the mainvbox file definition, 
>> rightpanevbox is the top widget? If you post the xml would help a lot. 
>> It seems to be an easy-to-solve problem :-)
>>
>> Good luck!
>>
>>
>> --
>> David
>>
>>
>> 2009/12/16 Andrew Wood <ajwood at theiet.org <mailto:ajwood at theiet.org>>
>>
>>     Im having problems getting libglade to load widgets from an XML file.
>>     My aim is to have a GtkVBox in a window - the window is made in
>>     code not
>>     Glade, and the contents of the vbox will be loaded from Glade
>>     files such
>>     that the contents of the vbox can change as the app runs.
>>
>>     To test it out I created 2 Glade XML files page1.glade and page2.glade
>>     each have the following hierarchy:  GtkWindow->GtkVBox->GtkLabel
>>
>>     The label contains the text "Page1" or "Page2" accordingly. I want to
>>     load the GtkVBox from either file and use it to 'replace' a manually
>>     created vbox which is in code.
>>
>>     In the code which is just a short 'main' file Ive declared the
>>     following:
>>
>>     GladeXML* page1xml;
>>     GladeXML* page2xml;
>>
>>     GtkWidget* mainwindow;
>>     GtkWidget* rightpanescrollview;
>>     GtkWidget* rightpanevbox;
>>
>>     page1xml=glade_xml_new("./page1.glade",NULL,NULL);
>>     rightpanevbox= glade_xml_get_widget(page1xml,"mainvbox");
>>
>>     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(rightpanescrollview),rightpanevbox);
>>
>>     The rightpanevbox is then put into the window.
>>
>>     But when the app runs the label from Glade doesnt appear and the
>>     following is printed on the console:
>>
>>     (a.out:3894): Gtk-CRITICAL **: gtk_scrolled_window_add_with_viewport:
>>     assertion `child->parent == NULL' failed
>>     I tried decalring in code a GtkWindow and loading the top-level window
>>     widget from the XML file but again I got an error similar but slightly
>>     different suggesting that no widgets are actually being found in
>>     the file.
>>
>>     The file itself is found because it throws up an error if I change the
>>     filename in the call to  glade_xml_new but it seemingly cant find
>>     anything in that file.
>>
>>     Any suggestions? I can post the entire main code file if it would help
>>     its not very long.
>>
>>     Thanks
>>     Andrew
>>
>>     _______________________________________________
>>     Glade-users maillist  -  Glade-users at lists.ximian.com
>>     <mailto:Glade-users at lists.ximian.com>
>>     http://lists.ximian.com/mailman/listinfo/glade-users
>>
>>
>>
>>
>> -- 
>> David
>>
>> ______________________________________________________________________
>> This email has been scanned by the MessageLabs Email Security System.
>> For more information please visit http://www.messagelabs.com/email
>> ______________________________________________________________________
> 
> _______________________________________________
> Glade-users maillist  -  Glade-users at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/glade-users
> 

I wrote a function getGladeWidget() to get the widget I want out of the
glade xml file.  Once I have it I add it to a container.  I'm not sure
how efficient it is, but it works.

I use it like this.
  wid=getGladeWidget(filename, "menubar1", TRUE);
  gtk_container_add(GTK_CONTAINER(vbox), wid);


The getGladeWidget() function is defined as follows.

GtkWidget *getGladeWidget(gchar *filename, gchar *glade_widget_id,
gboolean connect) {
  GladeXML *xml=NULL;
  GladeInterface *iface=NULL;
  GladeWidgetInfo *info=NULL;
  GtkWidget *rwidget=NULL; /* the return widget */

  xml=glade_xml_new(filename, glade_widget_id, NULL);

  iface=glade_parser_parse_file((filename, NULL);
  if (!iface) {
    g_object_unref(G_OBJECT(xml));
    g_error("%s:%d getGladeWidget() iface is NULL.\n",  __FILE__, __LINE__);
  }

  info=(GladeWidgetInfo *)g_hash_table_lookup(iface->names,
glade_widget_id);
  rwidget=glade_xml_build_widget(xml, info);
  gtk_widget_show(rwidget);

  if(connect==TRUE) {
    glade_xml_signal_autoconnect(xml); // Connect the Signals
  }

  glade_interface_destroy(iface);
  g_object_unref(G_OBJECT(xml));

  return rwidget;
}


Good Luck.

dhk




More information about the Glade-users mailing list