How to select the next row in a QTableView programmatically

I have a subclass of QTableView that im tags and saves its state with this:

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 ); } 

Now I like to programmatically move the selection to the next line (without clicking the line with the mouse) and calling clickedRowHandler (...) as a shell do I do this? Thanks

+4
source share
1 answer

You already have the index of the current row, so to get modelindex for the next row, use the following:

 QModelIndex next_index = table->model()->index(row + 1, 0); 

Then you can set this indexex model as current using

 table->setCurrentIndex(next_index); 

Obviously, you need to make sure that you don't go beyond the end of the table, and maybe there are some additional steps to make sure the entire row is selected, but this should help you closer.

+12
source

Source: https://habr.com/ru/post/1401096/


All Articles