如何在 Qt 中向上移动选定的行

New*_*oon 3 qt qtableview qstandarditemmodel qt4.8 qt5

我有一个QTableView3 行 2 列。(这里我使用的是QStandardItemModel)。我想在单击 QPushButton 时向上/向下移动一行。如何在 中向上/向下移动一行QTableView

感谢您的回复 vahancho。我已经尝试使用QAbstractItemModel::moveRow,但它不起作用:

   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());
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nov 6

使用 Qt 文档QStandartItemModel- QStandardItemModel 类

  1. takeRow
  2. insertRow