如何在Qt中上移选定的行 [英] How to Move Up a Selected Row in Qt

查看:481
本文介绍了如何在Qt中上移选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3行2列的QTableView. (此处使用的是QStandardItemModel).单击QPushButton时,我想向上/向下移动一行.如何在QTableView中上/下一行?

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?

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

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

推荐答案

将Qt文档用于QStandartItemModel- QStandardItemModel类

  1. takeRow
  2. insertRow
  1. takeRow
  2. insertRow

这篇关于如何在Qt中上移选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆