gpt4 book ai didi

c - 在 Gtk 中出现错误。什么原因?

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

我正在使用 Glade-3。我做了这个工具: enter image description here

我用名称 tut.glade 保存了项目并在文本编辑器中打开了文件,这是它的代码:

<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkListStore" id="liststore1"/>
<object class="GtkWindow" id="Home">
<property name="destroy_with_parent">True</property>
<signal name="destroy" handler="gtk_main_quit"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkTreeView" id="treeview2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
</object>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkVButtonBox" id="vbuttonbox2">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<property name="layout_style">center</property>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-goto-top</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-go-up</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label">gtk-add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button4">
<property name="label">gtk-remove</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button5">
<property name="label">gtk-go-down</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button6">
<property name="label">gtk-goto-bottom</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">5</property>
</packing>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkVBox" id="vbox2">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkViewport" id="viewport1">
<property name="visible">True</property>
<property name="resize_mode">queue</property>
<child>
<object class="GtkAlignment" id="alignment1">
<property name="width_request">0</property>
<property name="height_request">0</property>
<property name="visible">True</property>
<property name="xscale">0</property>
<property name="yscale">0</property>
<child>
<object class="GtkFrame" id="frame1">
<property name="visible">True</property>
<property name="label_xalign">0</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkDrawingArea" id="drawingarea1">
<property name="width_request">300</property>
<property name="height_request">200</property>
<property name="visible">True</property>
</object>
</child>
<child type="label_item">
<placeholder/>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkHButtonBox" id="hbuttonbox1">
<property name="visible">True</property>
<property name="spacing">2</property>
<property name="homogeneous">True</property>
<property name="layout_style">center</property>
<child>
<object class="GtkCheckButton" id="checkbutton2">
<property name="label" translatable="yes">Show points</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="checkbutton1">
<property name="label" translatable="yes">Show lines</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkStatusbar" id="statusbar1">
<property name="visible">True</property>
<property name="spacing">2</property>
</object>
<packing>
<property name="expand">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

然后我编写了一个名为 tut.c 的 C 程序,其代码如下:

#include <gtk/gtk.h>

int
main( int argc,
char **argv )
{
GtkBuilder *builder;
GtkWidget *window;
GError *error = NULL;

/* Init GTK+ */
gtk_init( &argc, &argv );

/* Create new GtkBuilder object */
builder = gtk_builder_new();
/* Load UI from file. If error occurs, report it and quit application.
* * Replace "tut.glade" with your saved project. */
if( ! gtk_builder_add_from_file( builder, "tut.glade", &error ) )
{
g_warning( "%s", error->message );
g_free( error );
return( 1 );
}

/* Get main window pointer from UI */
window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

/* Connect signals */
gtk_builder_connect_signals( builder, NULL );

/* Destroy builder, since we don't need it anymore */
g_object_unref( G_OBJECT( builder ) );

/* Show window. All other widgets are automatically shown by GtkBuilder */
gtk_widget_show( window );

/* Start main loop */
gtk_main();

return( 0 );
}

然后我用

编译了它

gcc -o tut tut.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)

然后我得到了名为tut 的可执行文件。我使用

执行它

./tut

我得到了这个错误:

(tut:19516): Gtk-CRITICAL **: gtk_widget_show: assertion `GTK_IS_WIDGET (widget)' failed

最佳答案

您正在请求名为 "window1" 的对象,但在您的 tut.glade 文件中没有类似的东西。该 XML 中只有一个窗口:

<object class="GtkWindow" id="Home">

所以尝试替换这个:

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

用这个:

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

也许对 window 进行 NULL 检查也是个好主意。

关于c - 在 Gtk 中出现错误。什么原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7049242/

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