我试图以编程方式滚动到包含在滚动窗口中的 Gtk::ColumnView 中的特定行和列。下面的代码确实正确获取并设置了调整(我通过 printf 语句验证了这一点),但滚动条不移动。
void MyGtk::Scrolled_Window_Scroll_To_Common(
Gtk::ScrolledWindow *scrolled,
int row,
int col,
int number_rows,
int number_cols
)
{
//
//First we must validate all parameters and terminate if any fail.
//
assert(scrolled);
assert(row >= 0);
assert(col >= 0);
assert(number_rows > 0);
assert(number_cols > 0);
//
//Now we need to fetch the horizontal and vertical adjustments for the
//scrolled window.
//
auto h_adjustment = scrolled->get_hadjustment();
assert(h_adjustment);
auto v_adjustment = scrolled->get_vadjustment();
assert(v_adjustment);
//
//Now we need to determnine the range for the horizontal scroll bar.
//
auto h_range =
h_adjustment->get_upper() -
h_adjustment->get_lower();
assert(h_range >= 0);
//
//Now we need to determnine the range for the vertical scroll bar.
//
auto v_range =
v_adjustment->get_upper() -
v_adjustment->get_lower();
assert(v_range >= 0);
//
//Now that we know the range for the horizontal scroll bar we can
//determine the value to set the adjustment to based on range, the
//column and total number of columns.
//
auto h_value =
static_cast<double>(col + 1) /
number_cols *
h_range +
h_adjustment->get_lower();
assert(h_value >= 0);
//
//Now that we know the range for the vertical scroll bar we can
//determine the value to set the adjustment to based on range, the
//row and total number of columns.
//
auto v_value =
static_cast<double>(row + 1) /
number_rows *
v_range +
v_adjustment->get_lower();
assert(v_value >= 0);
//
//Now that we have the horizontal value we can now adjust the value
//for the adjustment.
//
h_adjustment->set_value(h_value);
//
//Now that we have the vertical value we can now adjust the value
//for the adjustment.
//
v_adjustment->set_value(v_value);
//
//Now that we have changed the horizontal adjustment we need to set
//it for the horizontal scroll bar.
//
scrolled->set_hadjustment(h_adjustment);
//
//Now that we have changed the vertical adjustment we need to set
//it for the vertical scroll bar.
//
scrolled->set_vadjustment(v_adjustment);
}
我需要强制重绘滚动窗口吗?不太确定如何做到这一点,因此希望对上述代码有所帮助。
在此论坛上搜索此内容没有找到有效的结果。
要滚动到 Gtk::ScrolledWindow 内的 Gtk::ColumnView 中的特定行和列,请确保滚动窗口及其内容都是通过调用scrolled->realize() 来实现的。根据所需的行和列计算滚动位置,并使用scrolled->scroll_to(h_fraction, v_fraction)滚动到该位置,这会自动管理调整,无需手动调整。如果问题仍然存在,请检查您的代码和小部件层次结构是否存在可能影响滚动行为的任何潜在问题。