我想更改堆栈切换器项目的标题。

这是GUI Struct的摘录:

<child>
          <object class="GtkStack" id="stack1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkFixed" id="f1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </object>
              <packing>
                <property name="name">page2</property>
                <property name="title" translatable="yes">c</property>
              </packing>
            </child>
            <child>
              <object class="GtkFixed" id="f2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
              </object>
              <packing>
                <property name="name">page1</property>


这是代码:

GtkWidget       *window;
GtkWidget       *fixed1;
GtkBuilder      *builder;
GtkAdjustment   *adjustment1;
GtkWidget       *b1;
GtkWidget       *b2;
GtkWidget       *b3;
GtkWidget       *b4;
GtkWidget       *f1;
GtkWidget       *f2;
GtkWidget       *f3;
GtkWidget       *f4;
GtkWidget       *stack1;
GtkWidget       *stkSw;

int display = 4;

int main(int argc, char *argv[]) {
        gtk_init(&argc, &argv); // init Gtk

//---------------------------------------------------------------------
// establish contact with xml code used to adjust widget settings
//---------------------------------------------------------------------

        builder = gtk_builder_new_from_file ("scroll.glade");

        window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
        fixed1 = GTK_WIDGET(gtk_builder_get_object(builder, "fixed1"));

        b1 = GTK_WIDGET(gtk_builder_get_object(builder, "b1"));
        b2 = GTK_WIDGET(gtk_builder_get_object(builder, "b2"));
        b3 = GTK_WIDGET(gtk_builder_get_object(builder, "b3"));
        b4 = GTK_WIDGET(gtk_builder_get_object(builder, "b4"));
        f1 = GTK_WIDGET(gtk_builder_get_object(builder, "f1"));
        f2 = GTK_WIDGET(gtk_builder_get_object(builder, "f2"));
        f3 = GTK_WIDGET(gtk_builder_get_object(builder, "f3"));
        f4 = GTK_WIDGET(gtk_builder_get_object(builder, "f4"));
        stack1 = GTK_WIDGET(gtk_builder_get_object(builder, "stack1"));
        stkSw = GTK_WIDGET(gtk_builder_get_object(builder, "stkSw"));



        g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

        gtk_builder_connect_signals(builder, NULL);

        gtk_container_child_set_property (GTK_CONTAINER(stack1), f1, (const gchar*) "title", (const GValue*) "IVALUE");

        gtk_widget_show(window);

        gtk_main();

        return EXIT_SUCCESS;
}


插入此行会导致分段错误:

        gtk_container_child_set_property (GTK_CONTAINER(stack1), f1, (const gchar*) "title", (const GValue*) "IVALUE");


我根据Doc编写了这一行:
https://developer.gnome.org/gtk3/stable/GtkContainer.html#gtk-container-child-set-property

我没有根据上面的链接设置属性吗?还是我的代码中其他地方有错误?

最佳答案

要创建GValue,您不能只做:

(const GValue*) "IVALUE"


您必须初始化一个GValue,设置字符串,然后才能将其传递。最后,您必须取消设置(释放)GValue。

GValue a = G_VALUE_INIT;
g_value_init (&a, G_TYPE_STRING);
g_value_set_string (&a, "IVALUE");

gtk_container_child_set_property (GTK_CONTAINER(stack1), f1, (const gchar*) "title", &a);

g_value_unset (&a);


阅读此处以获取更多信息和更多示例:https://developer.gnome.org/gobject/stable/gobject-Generic-values.html

07-26 09:15