Question

I'm new in Gtk+ 3 programming and I'm trying to add pages into a notebook dynamically. I use glade to build the UI.

Here there's the code, but I obtain "invalid cast from 'GtkComboBoxText' to 'GtkNotebook'" error.

C code:

#include <gtk/gtk.h>

void
add_pages(GtkWidget *notebook) {
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *vbox2;
    GtkWidget *tab1_label;
    GtkWidget *tab2_label;

    vbox2 = gtk_vbox_new (FALSE,0);
    tab2_label = gtk_label_new ("Second page");
    gtk_widget_set_name (GTK_WIDGET(tab2_label), "second tab");
    gtk_notebook_insert_page (GTK_NOTEBOOK(notebook), GTK_WIDGET(vbox2), GTK_WIDGET(tab2_label), -1);
}

int main( int argc, char *argv[]) {
    GtkBuilder *gtkBuilder;
    GtkWidget *notebook;
    GtkWidget *page;

    GtkWidget *window;
    GtkWidget *combo_nodes;
    gtk_init(&argc, &argv);

    gtkBuilder = gtk_builder_new();
    gtk_builder_add_from_file(gtkBuilder, "main1.glade", NULL);

    gtk_builder_connect_signals(gtkBuilder, 0);
    combo_nodes = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "combo_nodes"));
    notebook = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "notebook"));
    page =  GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "page"));

    window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, "window"));
    g_signal_connect(combo_nodes, "changed", add_pages, notebook);

    g_object_unref(G_OBJECT(gtkBuilder));

    gtk_widget_show(window);
    gtk_main();

    return 0;
}

Glade code:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkListStore" id="datastore">
<columns>
  <!-- column-name città -->
  <column type="gchararray"/>
  <!-- column-name regione -->
  <column type="gchararray"/>
</columns>
</object>
<object class="GtkGrid" id="page">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
  <object class="GtkComboBoxText" id="combo_ports">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
  </object>
  <packing>
    <property name="left_attach">0</property>
    <property name="top_attach">0</property>
  </packing>
</child>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="window_position">center</property>
<property name="gravity">center</property>
<child>
  <object class="GtkNotebook" id="notebook">
    <property name="visible">True</property>
    <property name="can_focus">True</property>
    <child>
      <object class="GtkGrid" id="grid2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="column_homogeneous">True</property>
        <child>
          <object class="GtkComboBoxText" id="combo_nodes">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <items>
              <item translatable="yes">test</item>
            </items>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
      </object>
    </child>
    <child type="tab">
      <object class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Home</property>
      </object>
      <packing>
        <property name="tab_fill">False</property>
      </packing>
    </child>
    <child>
      <placeholder/>
    </child>
    <child type="tab">
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
    <child type="tab">
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
    <child type="tab">
      <placeholder/>
    </child>
  </object>
</child>
</object>
</interface>
Was it helpful?

Solution

That's a fairly clear error message... It's telling you that the 'notebook' pointer inside add_pages() is not really a GtkNotebook. If you then look at the documentation for GtkComboBox:changed you'll see that the signal signature looks like this:

void
user_function (GtkComboBox *widget,
               gpointer     user_data)

Your handler should have that signature as well (and user_data will then be a pointer to the GtkNotebook).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top