试图在Qt QTreeView中选择一整行
Trying to select a full row in a Qt QTreeView
我正在尝试使用以下代码选择QTreeView组件中的完整行:
const QModelIndex topLeft = model->index(0, 0);
const QModelIndex bottomRight = model->index(model->rowCount(), model->columnCount());
ui->hidDescriptorView->selectionModel()->selection().select(topLeft, bottomRight);
我有点无能,一直在寻找周围使用const_cast等,试图让选择工作,但编译器给了我以下错误:
/.../mainwindow.cpp:93: error: member function 'select' not viable: 'this' argument has type 'const QItemSelection', but function is not marked const
ui->hidDescriptorView->selectionModel()->selection().select(topLeft, bottomRight);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我来自之前的位,我设法做出一个选择,但只有一个单元格会被选中,所以我正在尝试上面的,以确保整个行被正确选择,就好像用户会点击它。
任何帮助将非常感激!
select()的签名为
const QItemSelection selection () const
。,您不能就地修改QItemSelection,因为它是一个const副本。作为一个副本,修改不会有任何影响。
相反,创建一个副本(或者只是创建一个新的QItemSelection)并通过select()传递它:QItemSelection selection = view->selectionModel()->selection();
selection.select(topLeft, bottomRight);
view->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
正如您所提到的,想要选择行,可能有更简单的方法:
view->selectionModel()->select(topLeft, QItemSelectionModel::ClearAndSelect|QItemSelectionModel::Rows);
将用户的选择扩展为整行:
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view.setCurrentIndex(
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 选择要调用的构造函数
- C++选择排序算法中的逻辑错误
- QTreeView幻灯片多选后无法使用单击选择
- 无法获取菜单选择以运行函数.C++
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在C++中,如何通过几种类型从元组中选择多个元素
- 如何使用QTreeView在QComboBox中选择项目
- 从 QTreeView 中删除项目时取消选择所有行
- QTreeView:如何中止选择更改
- QTreeView 项目悬停/基于当前颜色选择的背景颜色
- 如何在QTreeView中获取选择更改通知
- 是否可以从代理访问QTreeview的选择模型
- 如何配置QTreeView以在使用箭头键移动时保留多项选择
- 试图在Qt QTreeView中选择一整行
- Qt QTreeView -当条件不满足时恢复到以前的选择
- 如何在QTreeView中选择前一行
- 使用键盘箭头选择QTreeView中的项目
- QT上下文菜单只在QTreeView选择