提问人:Ahmad B 提问时间:6/4/2021 更新时间:6/14/2021 访问量:474
Gtk 树视图 要查看的列大小
Gtk Tree View columns size to view
问:
我是Gtk3的新手,但我在任何地方都找不到如何做我想做的事。我正在尝试获得表示文章引用的树视图。
预期结果:
- 列的大小是可调整的
- 无水平滚动条
- 列仅占用所需的空间,如果内容太长,它们不会展开窗口
- 如果单元格的内容太长,请将文本分几行(最好)或修剪并添加“...”(好吧),修剪它(我可以满足于此)
作为我想要的一个例子,这里是Zotero用“...”修剪列
这是我的小部件层次结构:
我尝试过什么(使用 Glade + C):
- 默认设置:出现水平滚动条
- 将 ScrolledWindow 水平滚动策略设置为“从不”,自动调整大小为列:窗口扩展以适合内容,例如
- 将 ScrolledWindow 水平滚动策略设置为“从不”,固定列大小,最大宽度:内容被修剪(不是“...”,没有文本重排),但我只能将列的大小调整为我给定的套利大小。
- 将 ScrolledWindow 水平滚动策略设置为“外部”,自动调整列大小,列设置为可调整大小:列不可调整大小,内容不修剪。
以下是变体 4,其中包含 Glade 生成的源 + XML 文件:
#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>
如果我错过了显而易见的事情,我深表歉意!
答:
0赞
theGtknerd
6/5/2021
#1
这可能更接近您在 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>
评论
0赞
Ahmad B
6/5/2021
啊差不多!(对于那些像我一样错过它的人,“......”options 是单元格渲染器的属性,而不是列;这称为省略号)。还有一个问题:调整列大小后,“...”只是消失了,溢出的内容被隐藏了。在这里进行演示。我们能否确保无论多么窄的柱子都会导致椭圆化?
0赞
theGtknerd
6/6/2021
将 ScrolledWindow 上的水平滚动属性更改为 。最后的Gtk.Treeview列有一些我还不完全理解的细微差别。您可能需要再添加一个空白列。Never
0赞
Ahmad B
6/14/2021
我尝试将水平滚动策略更改为从不;结果是一样的。我添加了一个空白列,但这也没有帮助,并且在调整大小时可以看到虚拟列。
0赞
theGtknerd
6/14/2021
尝试我的更改。你知道,你稍微摆弄一下可能是一个很好的学习工具。我们不能替代动手学习。
0赞
Ahmad B
6/17/2021
我知道摆弄的教育价值,谢谢!我确实尝试了许多修改,使用您的原始 Glade 文件和评论作为起点。我实际上提出了您的建议,但对在某个点之后调整列大小会调整整个窗口大小这一事实感到不满意。由于这个 desideratum 不是原始问题的一部分,我将验证您的答案。
评论