gpt4 book ai didi

c - Gtk+ 3 笔记本 : how to add pages dynamically?

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:19 25 4
gpt4 key购买 nike

我是 Gtk+ 3 编程的新手,我正在尝试将页面动态添加到笔记本中。我使用 glade 构建 UI。

这是代码,但我收到“从‘GtkComboBoxText’到‘GtkNotebook’的无效转换”错误。

C 代码:

#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;
}

空地代码:

<?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>

最佳答案

这是一个相当明确的错误消息...它告诉您 add_pages() 中的“notebook”指针并不是真正的 GtkNotebook。如果您随后查看 GtkComboBox:changed 的文档,您会看到信号签名如下所示:

void
user_function (GtkComboBox *widget,
gpointer user_data)

您的处理程序也应该具有该签名(user_data 将成为指向 GtkNotebook 的指针)。

关于c - Gtk+ 3 笔记本 : how to add pages dynamically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com