C++ 如何以编程方式选择QTableView中的下一行
我有C++ 如何以编程方式选择QTableView中的下一行,c++,qt,C++,Qt,我有QTableView子类,我正在用它标记并保存它的状态: connect(this, SIGNAL(clicked(const QModelIndex &)), this, SLOT(clickedRowHandler(const QModelIndex &)) ); void PlayListPlayerView::clickedRowHandler(const QModelIndex & index) {
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(…)
我该怎么做?
谢谢您已经有了当前的行索引,因此请使用类似于以下内容的内容来获取下一行的modelindex
QModelIndex next_index = table->model()->index(row + 1, 0);
然后,您可以使用
table->setCurrentIndex(next_index);
显然,您需要确保您没有跑过表的末尾,并且可能还有一些额外的步骤来确保选中整行,但这会让您更靠近。感谢重播,我如何使它被选中(行上有蓝色?table->selectionModel()->select(索引,QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows)您好,谁知道如何选择下一行/上一行跳过不可选择的行?因此它将模拟按下向下/向上箭头。当模型有没有Qt::ItemIsSelectable或Qt::ItemIsEnabled标志的行时,上面的代码不起作用。
/*
* 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 );
}