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());
If you use Qt5 you can take a look on this function:
"On models that support this, moves sourceColumn from sourceParent to destinationChild under destinationParent. Returns true if the columns were successfully moved; otherwise returns false."