【问题标题】:Is there an easy way to get a textual representation of a GtkWidgetPath?有没有一种简单的方法来获取 GtkWidgetPath 的文本表示?
【发布时间】:2013-08-11 00:00:06
【问题描述】:

我没有为GtkWidgetPath 找到任何 *_to_string() 函数,那么如何轻松检查其内容以调试我遇到的问题?在控制台上打印它的文本表示会很棒。

【问题讨论】:

    标签: css gtk gtk3


    【解决方案1】:

    如果我理解你是正确的,你想写一些类似的东西

    <Widget>#<Name>: <NameOfObject>#<Name>
        The widget belongs to the following style classes: <listOfClasses>
        Complete Path: <Path>
    

    用于屏幕上的特定小部件。


    我编写了以下两个函数来完成这项工作。代码应该可以很容易地翻译成其他语言,并且如果您手头有参考资料,则一目了然。

    gchar *
    widget_path_get_style_classes (GtkWidgetPath * path)
    {
        GSList *list;
        gchar *str = NULL, *ptr;
    
        list = gtk_widget_path_iter_list_classes (path, -1);
    
        if (list == NULL)
            return NULL;
    
        do {
    
            ptr = str;
            str = g_strdup_printf (".%s", (gchar *) list->data);
            str = g_strjoin (", ", str, ptr, NULL);
            g_free (ptr);
    
        } while (list = g_slist_next (list));
    
        g_slist_free(list);
    
        return str;
    }
    
    void
    widget_path_dumper (GtkWidget * widget)
    {
    
        GtkWidgetPath *path;
        guint i, length;
        gchar *str;
        GType type;
    
        path = gtk_widget_get_path (widget);
        length = gtk_widget_path_length (path) - 1;
    
        type = gtk_widget_path_iter_get_object_type (path, length);
        str = (gchar *) gtk_widget_path_iter_get_name (path, length);
    
        if (str != NULL)
            g_print ("<Widget>#<Name>: %s#%s\n", g_type_name (type), str);
        else
            g_print("<Widget>: %s\n", g_type_name(type));
    
        str = widget_path_get_style_classes (path);
    
        if (str != NULL) {
            g_print
                ("\tThe widget belongs to the following style classes: %s\n", str);
            g_free (str);
        }
    
        g_print ("\tComplete Path: ");
    
        for (i = 0; i <= length; i++) {
    
            type = gtk_widget_path_iter_get_object_type (path, i);
            g_print ("/%s", g_type_name (type));
    
        }
    
        g_print ("\n");
    }
    

    示例:

    如果您在它放置的附加 ui 文件的实例化 button1 小部件上调用 widget_path_dumper

    <Widget>#<Name>: GtkButton#myspecialbutton
        The widget belongs to the following style classes: .button
        Complete Path: /GtkWindow/GtkGrid/GtkFrame/GtkAlignment/GtkButton
    

    在屏幕上。

    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <!-- interface-requires gtk+ 3.0 -->
      <object class="GtkWindow" id="window1">
        <property name="can_focus">False</property>
        <property name="border_width">10</property>
        <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
        <child>
          <object class="GtkGrid" id="grid1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkFrame" id="frame1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label_xalign">0</property>
                <property name="shadow_type">out</property>
                <child>
                  <object class="GtkAlignment" id="alignment1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="margin_right">10</property>
                    <property name="margin_top">10</property>
                    <property name="margin_bottom">10</property>
                    <property name="left_padding">12</property>
                    <child>
                      <object class="GtkButton" id="button1">
                        <property name="label" translatable="yes">button</property>
                        <property name="name">myspecialbutton</property>
                        <property name="use_action_appearance">False</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="halign">center</property>
                        <property name="valign">center</property>
                        <property name="use_action_appearance">False</property>
                      </object>
                    </child>
                  </object>
                </child>
                <child type="label">
                  <object class="GtkLabel" id="label1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">&lt;b&gt;frame1&lt;/b&gt;</property>
                    <property name="use_markup">True</property>
                  </object>
                </child>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">1</property>
                <property name="width">1</property>
                <property name="height">1</property>
              </packing>
            </child>
          </object>
        </child>
      </object>
    </interface>
    

    【讨论】:

    • 非常感谢,只有 nitpick 是我在编译 helloworld.c: In function ‘widget_path_get_style_classes’: helloworld.c:18:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘gpointer’ [-Wformat] 时收到的警告
    • 我忘记将gpointer 转换为gchar *。我会相应地更新代码。
    【解决方案2】:

    从gtk v3.2开始就有gtk_widget_path_to_string,可以这么简单的使用:

    printf("%s\n",gtk_widget_path_to_string (path));
    

    .. 但是与 user1146332 的提议相比,输出看起来并不那么漂亮

    【讨论】:

      猜你喜欢
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 2011-01-07
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多