使用GtkBuilder时无法更改GtkWindow大小

时间:2014-05-08 01:31:03

标签: c user-interface gtk gtk3 glade

我使用GtkBuilder和Glade XML文件。我想在不使用gtk_window_fullscreen方法的情况下将窗口大小更改为全屏(因为这需要在没有窗口管理器的情况下工作),因此我得到屏幕尺寸并使窗口大小。当我以编程方式创建小部件时,这常常起作用,但是当我切换到Glade / GtkBuilder时,窗口的大小与Glade编辑器中的小小一样。我是否必须使用GtkBuilder做一些不同的事情?这是代码:

#include <gtk/gtk.h>

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

    GtkBuilder *builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "dm.ui", NULL);

    GObject *window = gtk_builder_get_object(builder, "window");

    // Make full screen
    GdkScreen *screen = gdk_screen_get_default();
    gint height = gdk_screen_get_height(screen);
    gint width = gdk_screen_get_width(screen);
    gtk_window_set_default_size(GTK_WINDOW(window), width, height);

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

    gtk_main();
}

这是XML文件,dm.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.2 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkWindow" id="window">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkLayout" id="main_layout">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkEntry" id="user_text_field">
            <property name="width_request">162</property>
            <property name="height_request">31</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="margin_right">42</property>
          </object>
          <packing>
            <property name="x">175</property>
            <property name="y">73</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="user_label">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Username</property>
          </object>
          <packing>
            <property name="x">71</property>
            <property name="y">46</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="password_label">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Password</property>
          </object>
          <packing>
            <property name="x">72</property>
            <property name="y">103</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="password_text_field">
            <property name="width_request">162</property>
            <property name="height_request">31</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="x">177</property>
            <property name="y">129</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:2)

您可以尝试使用

void gtk_widget_set_size_request(GtkWidget *widget, gint width, gint height);

或者您可以使用Glade调整“default-width”和“default-height”的GtkWindow属性,而不是硬编码。 让我知道结果。 p.s:这就是我讨厌格莱德的原因:(