I have a QTableView
with 3 rows and 2 columns. (Here I am using a QStandardItemModel
). I want to move up/move down a single row when a QPushButton is clicked. How can I move up/down a row in QTableView
?
Thanks for your reply vahancho. I have already tried using QAbstractItemModel::moveRow
, but it doesn't work:
int currentRow = ui->tableView->currentIndex().row();
QModelIndex sourceParent = ui->tableView->model()->index(ui->tableView->selectionModel()->currentIndex().row(),0);
QModelIndex destinationParent = ui->tableView->model()->index(ui->tableView->selectionModel()->currentIndex().row()+1,0);
ui->tableView->model()->moveRow(sourceParent,currentRow, destinationParent,destinationParent.row());
Use Qt documentation for QStandartItemModel
- QStandardItemModel Class
takeRow
insertRow