如何以编程方式选择QTableView中的下一行
How to select next row in QTableView programmatically
我有一个QTableView
子类,我正在标记它并用这个保存它的状态:
connect(this,
SIGNAL(clicked(const QModelIndex &)),
this,
SLOT(clickedRowHandler(const QModelIndex &))
);
void PlayListPlayerView::clickedRowHandler(const QModelIndex & index)
{
int iSelectedRow = index.row();
QString link = index.model()->index(index.row(),0, index.parent()).data(Qt::UserRole).toString();
emit UpdateApp(1,link );
}
现在我喜欢用程序将所选内容移动到下一行(而不是用鼠标按行)并调用clickedRowHandler(...)
,我该怎么做?感谢
您已经有了当前行索引,所以使用以下内容来获取下一行的模型索引
QModelIndex next_index = table->model()->index(row + 1, 0);
然后您可以使用将该模型索引设置为当前模型索引
table->setCurrentIndex(next_index);
显然,你需要确保你没有跑过表的末尾,而且可能还有一些额外的步骤来确保整行都被选中,但这应该会让你更接近。
/*
* selectNextRow() requires a row based selection model.
* selectionMode = SingleSelection
* selectionBehavior = SelectRows
*/
void MainWindow::selectNextRow( QTableView *view )
{
QItemSelectionModel *selectionModel = view->selectionModel();
int row = -1;
if ( selectionModel->hasSelection() )
row = selectionModel->selection().first().indexes().first().row();
int rowcount = view->model()->rowCount();
row = (row + 1 ) % rowcount;
QModelIndex newIndex = view->model()->index(row, 0);
selectionModel->select( newIndex, QItemSelectionModel::ClearAndSelect );
}
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 从C++dll访问C#中的一行主要参数
- 在C++中,我如何接受不同于同一行的用户输入
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 如何从一行中读取特定数量的字符?(C++)
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 读取最后一行代码算法 - c++ 时出现问题
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 模板元编程 - 尝试实现维度分析
- 有没有办法在一行中填充矢量图
- 需要将一行代码从C++ Qt翻译成PyQt
- 我是编程新手
- 为什么如果我添加这一行,我的程序会不断询问值
- C++编程从外部文本文件定义数组大小
- 在C++编程中继续下一行的另一种方法是什么?
- 如何以编程方式选择QTableView中的下一行