How to select next row in QTableView programmatically

后端 未结 2 687
-上瘾入骨i
-上瘾入骨i 2020-12-19 04:14

I have QTableView subclass that I am marking and saving its state with this :

connect(this,
        SIGNAL(clicked(const QModelIndex &)),
           


        
2条回答
  •  醉梦人生
    2020-12-19 04:45

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

提交回复
热议问题