Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select next row in QTableView programmatically

Tags:

c++

qt

I have QTableView subclass that I am marking and saving 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 programmatically to move the selection to the next row (not by pressing the row with the mouse) and invoking clickedRowHandler(...) how shall i do that ? Thanks

like image 616
user63898 Avatar asked Mar 13 '12 03:03

user63898


2 Answers

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

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

Then you can set that modelindex as the current one using

table->setCurrentIndex(next_index);

Obviously you'll need to make sure you're not running past the end of the table, and there's probably some extra steps to make sure the entire row is selected, but that should get you closer.

like image 75
aldo Avatar answered Nov 07 '22 16:11

aldo


/*
 * 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 );
}
like image 42
ClearCrescendo Avatar answered Nov 07 '22 17:11

ClearCrescendo