How to Move Up a Selected Row in Qt

New Moon picture New Moon · Sep 9, 2013 · Viewed 9.8k times · Source

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

Answer

Dmitry Sazonov picture Dmitry Sazonov · Sep 9, 2013

Use Qt documentation for QStandartItemModel - QStandardItemModel Class

  1. takeRow
  2. insertRow