要查看的 Gtk 树视图列大小

Gtk Tree View columns size to view

我是 Gtk3 的新手,但我无法在任何地方找到如何做我想做的事。我正在尝试获取表示文章引用的树视图。

期望的结果:

作为我想要的例子,这里是 Zotero trimming columns with "..."

这是我的小部件层次结构:

我尝试了什么(使用 Glade + C):

  1. 默认设置:出现水平滚动条
  2. 将 ScrolledWindow 水平滚动策略设置为“从不”,自动调整列大小:window 扩展以适应内容,例如
  3. 将 ScrolledWindow 水平滚动策略设置为“从不”,固定大小的列,最大宽度:内容是 trimmed(不是“...”,没有文本重排),但我只能调整列的大小到我给他们的任意大小。
  4. 将 ScrolledWindow 水平滚动策略设置为“外部”,自动调整列大小,列设置为可调整大小:列不可可调整大小,内容不可trimmed。

这是变体 4,源代码 + XML Glade 生成的文件:

#include <gtk/gtk.h>

void on_window_main_destroy();

enum {
    AUTHORS_COLUMN = 0,
    TITLE_COLUMN,
    N_COLUMNS
};

static void init_reference_list(GtkBuilder* builder)
{
    GtkCellRenderer*   renderer;
    GtkWidget*         tree_widget;
    tree_widget = GTK_WIDGET(gtk_builder_get_object(builder, "tree_view"));

    // -- Add attributes to Glade-generated columns
    GtkTreeViewColumn* column;
    renderer = gtk_cell_renderer_text_new();
    column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "title"));
    gtk_tree_view_column_pack_start(column, renderer, FALSE);
    gtk_tree_view_column_add_attribute(column, renderer, "text", TITLE_COLUMN ); 
    column = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder, "authors"));
    gtk_tree_view_column_pack_start(column, renderer, FALSE);
    gtk_tree_view_column_add_attribute(column, renderer, "text", AUTHORS_COLUMN );
    // -- Set up store and connect it to view
    GtkListStore* store;
    store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING ); 
    gtk_tree_view_set_model(GTK_TREE_VIEW(tree_widget), GTK_TREE_MODEL(store));


    // -- Placing fake data in store
    char title[]   =  "A long long long long long long long long long long long long long long long long long long long long long long long long  long long long long long long long long long long title";
    char authors[] =  "Me You Us Them All of us";
    GtkTreeIter iter;
    int i;
    for (int i = 0; i < 500; ++i)
    {
        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter, AUTHORS_COLUMN, authors, TITLE_COLUMN,   title, -1 );
    }

}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}


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

        // -- load builder
        GtkBuilder* builder; 
        builder = gtk_builder_new();
        gtk_builder_add_from_file (builder, "resources/ui/ref_search.glade", NULL);
        gtk_builder_connect_signals(builder, NULL);

        // -- Cell Renderer and fake data
        init_reference_list(builder);

        // -- Launch
        GtkWidget         *window;
        window  = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
        gtk_widget_show(window);     
        g_object_unref(builder);
        gtk_main();

        return 0;
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkEntryBuffer" id="search_buffer"/>
  <object class="GtkWindow" id="window_main">
    <property name="can_focus">False</property>
    <property name="halign">baseline</property>
    <property name="valign">baseline</property>
    <property name="title" translatable="yes">Reference Manager</property>
    <property name="resizable">False</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkSearchEntry" id="search_bar">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="buffer">search_buffer</property>
            <property name="primary_icon_name">edit-find-symbolic</property>
            <property name="primary_icon_activatable">False</property>
            <property name="primary_icon_sensitive">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">external</property>
            <property name="shadow_type">in</property>
            <property name="max_content_height">500</property>
            <child>
              <object class="GtkTreeView" id="tree_view">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="rules_hint">True</property>
                <property name="enable_search">False</property>
                <property name="show_expanders">False</property>
                <property name="enable_grid_lines">horizontal</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="authors">
                    <property name="resizable">True</property>
                    <property name="sizing">autosize</property>
                    <property name="title" translatable="yes">Author(s)</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="title">
                    <property name="resizable">True</property>
                    <property name="sizing">autosize</property>
                    <property name="title" translatable="yes">Title</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

抱歉,如果我遗漏了显而易见的内容!

这可能更接近您在 Glade 文件中寻找的内容:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name authors -->
      <column type="gchararray"/>
      <!-- column-name titles -->
      <column type="gchararray"/>
    </columns>
    <data>
      <row>
        <col id="0" translatable="yes">Me You Us Them All of us</col>
        <col id="1" translatable="yes">A long long long long long long long long long long long long long long long long long long long long long long long long  long long long long long long long long long long title</col>
      </row>
    </data>
  </object>
  <object class="GtkEntryBuffer" id="search_buffer"/>
  <object class="GtkWindow" id="window_main">
    <property name="can_focus">False</property>
    <property name="halign">baseline</property>
    <property name="valign">baseline</property>
    <property name="title" translatable="yes">Reference Manager</property>
    <property name="resizable">False</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkSearchEntry" id="search_bar">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="buffer">search_buffer</property>
            <property name="primary_icon_name">edit-find-symbolic</property>
            <property name="primary_icon_activatable">False</property>
            <property name="primary_icon_sensitive">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">external</property>
            <property name="shadow_type">in</property>
            <property name="max_content_height">500</property>
            <child>
              <object class="GtkTreeView" id="tree_view">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="model">liststore1</property>
                <property name="rules_hint">True</property>
                <property name="enable_search">False</property>
                <property name="show_expanders">False</property>
                <property name="enable_grid_lines">horizontal</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="authors">
                    <property name="resizable">True</property>
                    <property name="sizing">fixed</property>
                    <property name="title" translatable="yes">Author(s)</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                    <child>
                      <object class="GtkCellRendererText">
                        <property name="ellipsize">middle</property>
                      </object>
                      <attributes>
                        <attribute name="text">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="title">
                    <property name="resizable">True</property>
                    <property name="sizing">fixed</property>
                    <property name="title" translatable="yes">Title</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                    <child>
                      <object class="GtkCellRendererText">
                        <property name="ellipsize">middle</property>
                      </object>
                      <attributes>
                        <attribute name="text">1</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

再次尝试编辑:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name authors -->
      <column type="gchararray"/>
      <!-- column-name titles -->
      <column type="gchararray"/>
    </columns>
    <data>
      <row>
        <col id="0" translatable="yes">Me You Us Them All of us</col>
        <col id="1" translatable="yes">A long long long long long long long long long long long long long long long long long long long long long long long long  long long long long long long long long long long title</col>
      </row>
    </data>
  </object>
  <object class="GtkEntryBuffer" id="search_buffer"/>
  <object class="GtkWindow" id="window_main">
    <property name="width_request">250</property>
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Reference Manager</property>
    <property name="resizable">False</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkSearchEntry" id="search_bar">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="buffer">search_buffer</property>
            <property name="primary_icon_name">edit-find-symbolic</property>
            <property name="primary_icon_activatable">False</property>
            <property name="primary_icon_sensitive">False</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">never</property>
            <property name="shadow_type">in</property>
            <property name="max_content_height">500</property>
            <child>
              <object class="GtkTreeView" id="tree_view">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="model">liststore1</property>
                <property name="rules_hint">True</property>
                <property name="enable_search">False</property>
                <property name="show_expanders">False</property>
                <property name="enable_grid_lines">horizontal</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="authors">
                    <property name="resizable">True</property>
                    <property name="sizing">fixed</property>
                    <property name="min_width">25</property>
                    <property name="title" translatable="yes">Author(s)</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                    <child>
                      <object class="GtkCellRendererText">
                        <property name="ellipsize">middle</property>
                      </object>
                      <attributes>
                        <attribute name="text">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="title">
                    <property name="resizable">True</property>
                    <property name="min_width">25</property>
                    <property name="title" translatable="yes">Title</property>
                    <property name="expand">True</property>
                    <property name="clickable">True</property>
                    <child>
                      <object class="GtkCellRendererText">
                        <property name="ellipsize">middle</property>
                      </object>
                      <attributes>
                        <attribute name="text">1</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>