因为用的是mvc,也不需要设置拖动样式之类的,所以这里不用重写drop的4大操作, 即enter leave move drop。以下内容来自龚建波大佬的测试案例。
QTableView* tableview = new QTableView(this);
//一、tableview拖动设置(默认拖动单元格)
tableview->setDragEnabled(true);
tableview->setSelectionMode(QAbstractItemView::SingleSelection); //拖动行列必选
tableview->setSelectionBehavior(QAbstractItemView::SelectRows); //拖动行
//tableview->setSelectionBehavior(QAbstractItemView::SelectColumns); //拖动列
tableview->setDefaultDropAction(Qt::MoveAction);
tableview->setDragDropMode(QAbstractItemView::InternalMove);
//二、tableview模型拖动设置
Qt::DropActions supportedDropActions() const override;
Qt::DropActions tableModel::supportedDropActions() const
{
return Qt::MoveAction | QAbstractTableModel::supportedDropActions();
}
//drag时携带的信息
QMimeData *mimeData(const QModelIndexList &indexes) const override;
QMimeData *tableModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *data=QAbstractTableModel::mimeData

本文介绍了一种在Qt环境中使用QTableView实现数据行拖动的方法。通过设置tableview的拖动和选择模式,以及重写模型的拖放相关方法,实现了内部移动模式的数据行拖动。在拖放过程中,数据模型根据拖动信息进行相应的数据交换,允许用户方便地调整数据行顺序。
最低0.47元/天 解锁文章
3939





