其他分享
首页 > 其他分享> > c – gnome-builder找不到我的glade文件

c – gnome-builder找不到我的glade文件

作者:互联网

我尝试用gnome-builder编写GUI程序,并尝试使用Glade进行布局.我在Programmer’s Notes上做了一个教程.

C

#include <gtk/gtk.h>

int main(int argc, char *argv[]){


    GError          *err = NULL;
    GtkBuilder      *builder;
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "window_main.ui", &err);

    if (err != NULL) {
        fprintf (stderr, "Unable to read file: %s\n", err->message);
        g_error_free(err);
        return 1;
    }

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

    if (window == NULL || !GTK_IS_WINDOW(window)) {
        fprintf (stderr, "Unable to get window. (window == NULL || window != GtkWindow)\n");
        return 1;
    }

    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);
    gtk_main();

    return 0;
}

void on_window_main_destroy(GtkWidget *widget, gpointer user_data){
    gtk_main_quit();
}

林间空地

<?xml version=1.0 encoding="UFT-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
    <requires lib="gtk+" version="3.12"/>
    <object class="GtkWindow" id="window_main">
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">Test Window</property>
        <property name="default_width">640</property>
        <property name="default_height">480</property>
        <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
        <child>
            <placeholder/>
     </child>
    </object>
</interface>

.glade文件位于src / direction中,就像.c文件一样.
当我运行代码时,返回以下错误消息
无法读取文件:无法打开文件“./window_main.ui”:没有此类文件或目录

我不知道应该把林间文件放在哪里,因为当我编译自己的时候
gcc main.c -Wall’pkg-config –cflags –libs gtk -3.0′-export-dynamic
并使用./a.out运行它,窗口打开时没有任何错误.

解决方法:

正如评论区域中TingPing所提到的,最好的方法是将其包含在GResource中.在您的情况下,资源规范XML就像

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/main">
    <file>window_main.ui</file>
  </gresource>
</gresources>

然后用gtk_builder_new_from_resource()加载资源,就像

GtkBuilder *builder = gtk_builder_new_from_resource("/main/window_main.ui");

标签:c-3,linux,gtk,glade,gnome-builder
来源: https://codeday.me/bug/20190627/1306349.html