侏儒生成者找不到我的林间文件

时间:2017-08-26 09:57:21

标签: c linux gtk glade gnome-builder

我尝试用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文件一样。 当我运行代码时,返回以下错误消息 Unable to read file: Failed to open file “./window_main.ui”: No such file or directory 我不知道应该把林间空档文件放在哪里,因为当我编译自己的时候 gcc main.c -Wall 'pkg-config --cflags --libs gtk+-3.0' -export-dynamic 并使用./a.out运行它,窗口打开时没有任何错误。

1 个答案:

答案 0 :(得分:3)

正如评论区域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");