gpt4 book ai didi

c - GTKScrolled Window 中的 GTK3 C 程序按钮,带有 GTKTreeView 和 GTKListStore,可将行滚动到 View 中

转载 作者:行者123 更新时间:2023-11-30 16:05:38 28 4
gpt4 key购买 nike

我用 C 语言将一个程序移植到 GTK3,除了一件事之外,一切都很完美。假设 TreeView 一次只显示 10 行,但填充了 100 行,其他行当然不在 View 中, TreeView 设置为单选多列行。

我使用按钮回调函数突出显示并选择第 0 行,然后在调用结束时增加静态索引++。按下“下一步”按钮会突出显示并选择第 1 行,依此类推。

当突出显示和选定的行到达第 11 行(不在 View 中)时,我不知道如何滚动 TreeView 以便下一个第 11 行进入 View 。这些行不断突出显示,因为我有 printf 语句用于反馈。我用谷歌搜索了几个星期,看到了例子和所有的东西,但对C来说不是那么多,虽然我必须将一些其他代码翻译成C,我用来弄清楚如何选择和突出显示行,但在C中无法弄清楚滚动部分。

使用 Glade,我有一个带有子 GtkFixed 的主窗口,带有按钮、进度栏,GtkFixed 中是一个 GtkScrolledWindow,其中包含一个 GtkViewPort,其中包含基于字符串的列的 GtkTreeView。

感谢您的任何反馈和/或链接。不确定 TreeView 是否需要位于 GtkViewPort 内,但可能在开发和研究过程中查看了旧代码。

我的回调函数

void single_button_clicked_cb(GtkWidget *widget, gpointer data)
{
static int index = 0;
GtkTreeIter iter;
char buf[9];
GtkTreeModel *model;
GtkAdjustment *vadjustment;
int i;

printf("\nScroll Active test row to %d\n",index);

sprintf(buf,"%d",index);

gtk_widget_grab_focus (GTK_WIDGET(gtk_tree_view));

GtkTreePath *path = gtk_tree_path_new_from_string(buf);

gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW (gtk_tree_view),path, NULL,FALSE, 0.0, 0.0);

gtk_tree_view_set_cursor (gtk_tree_view,path,NULL,FALSE); // works but doesn't scroll

gtk_tree_path_free(path);

index++;
}

林地文件

<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<!-- interface-naming-policy toplevel-contextual -->
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
</object>
<object class="GtkListStore" id="gtk_list_store">
<columns>
<!-- column-name Test -->
<column type="gchararray"/>
<!-- column-name Test1 -->
<column type="gchararray"/>
<!-- column-name Test2 -->
<column type="gchararray"/>
<!-- column-name Test3 -->
<column type="gchararray"/>
<!-- column-name Test4 -->
<column type="gchararray"/>
<!-- column-name Pass -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="main_window">
<property name="width_request">1050</property>
<property name="height_request">810</property>
<property name="can_focus">False</property>
<property name="border_width">3</property>
<property name="resizable">False</property>
<property name="window_position">center</property>
<property name="gravity">center</property>
<signal name="destroy" handler="on_main_window_destroy" swapped="no"/>
<child>
<object class="GtkFixed" id="gtk_fixed">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="start_button">
<property name="label" translatable="yes">Start</property>
<property name="width_request">100</property>
<property name="height_request">48</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="start_button_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="x">935</property>
<property name="y">605</property>
</packing>
</child>
<child>
<object class="GtkButton" id="single_button">
<property name="label" translatable="yes">Single</property>
<property name="width_request">100</property>
<property name="height_request">48</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="single_button_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="x">6</property>
<property name="y">605</property>
</packing>
</child>
<child>
<object class="GtkProgressBar" id="progress_bar">
<property name="width_request">1030</property>
<property name="height_request">25</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="x">1</property>
<property name="y">100</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolled_window">
<property name="width_request">1030</property>
<property name="height_request">465</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="vadjustment">adjustment1</property>
<property name="hscrollbar_policy">never</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkViewport" id="gtk_viewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkTreeView" id="gtk_tree_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">gtk_list_store</property>
<property name="headers_clickable">False</property>
<property name="enable_grid_lines">both</property>
<signal name="cursor-changed" handler="selected_row_index_cb" swapped="no"/>
<child>
<object class="GtkTreeViewColumn" id="col_test_description">
<property name="resizable">True</property>
<property name="title" translatable="yes">Test Description</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_description"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_test_number">
<property name="resizable">True</property>
<property name="title" translatable="yes">Test Number</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_number"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_test_limits">
<property name="resizable">True</property>
<property name="title" translatable="yes">Test Limits</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_limits"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_test_measurement">
<property name="resizable">True</property>
<property name="title" translatable="yes">Test Measurement</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_measurement"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_test_units">
<property name="resizable">True</property>
<property name="title" translatable="yes">Test Units</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_units"/>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_test_pass_fail">
<property name="resizable">True</property>
<property name="title" translatable="yes">Pass / Fail</property>
<property name="alignment">0.5</property>
<child>
<object class="GtkCellRendererText" id="col_renderer_pass_fail"/>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="x">3</property>
<property name="y">132</property>
</packing>
</child>
<child>
<object class="GtkMenuBar" id="menu">
<property name="width_request">180</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="gtk_file_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="gtk_file_menu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="gtk_new">
<property name="label">gtk-new</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="gtk_open">
<property name="label">gtk-open</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="open_limits_file" swapped="no"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="gtk_save">
<property name="label">gtk-save</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="gtk_save_as">
<property name="label">gtk-save-as</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="gtk_quit">
<property name="label">gtk-quit</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="on_main_window_destroy" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="gtk_edit_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Edit</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="gtk_edit_menu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem6">
<property name="label">gtk-cut</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem7">
<property name="label">gtk-copy</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem8">
<property name="label">gtk-paste</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem9">
<property name="label">gtk-delete</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="gtk_view_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_View</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="gtk_help_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="gtk_help_menu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem10">
<property name="label">gtk-about</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="x">-1</property>
<property name="y">-1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="test_name">
<property name="width_request">1030</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_markup">True</property>
<property name="justify">center</property>
<property name="single_line_mode">True</property>
</object>
<packing>
<property name="x">3</property>
<property name="y">667</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="test_command">
<property name="width_request">1030</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_markup">True</property>
<property name="justify">center</property>
<property name="single_line_mode">True</property>
</object>
<packing>
<property name="x">3</property>
<property name="y">32</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

最佳答案

终于找到问题了。我发现的另一个例子将 TreeView 作为视口(viewport)的 subview 。也许来自 GTK 的早期版本。

来自文档 Gnome Developer Documentation for GtkScrolledWindow

滚动窗口有两种工作方式。一些小部件具有 native 滚动支持;这些小部件实现了 GtkScrollable 接口(interface)。具有 native 滚动支持的小部件包括 GtkTreeView、GtkTextView 和 GtkLayout。

对于缺乏 native 滚动支持的小部件,GtkViewport 小部件充当适配器类,为缺乏自己的滚动功能的子小部件实现可滚动性。使用 GtkViewport 滚动子部件,例如 GtkGrid、GtkBox 等。

关于c - GTKScrolled Window 中的 GTK3 C 程序按钮,带有 GTKTreeView 和 GTKListStore,可将行滚动到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60232124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com