从继承的QTableView拖动

时间:2015-01-19 17:03:12

标签: qt

我在我的应用程序中有一个名为“partsview”的表视图,它来自QTableView,这是在主窗体上。相关的标题代码如下所示:

class partsiew : public QTableView
{
    Q_OBJECT
public:
    explicit partsview(QWidget *parent = 0);
public:
    QStringList mimeTypes() const;
    QMimeData *mimeData(const QModelIndexList &indexes) const;
};

我还在“partsview”的构造函数中添加了以下内容:

this->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->setDragEnabled(true);
this->setAcceptDrops(true);
this->setDropIndicatorShown(true);

尽管可能不需要最后两个。

拖动时,我可以选择一行并将其拖动到目标 - QTreeView - 我会得到相应的光标,甚至会在dropMimeData上触发treeview事件

但是,dropMimeData()方法中的行和列值始终为-1,并且不会调用“partsview”中的两个方法。

我猜第一个错误可能与第二个错误有关。我是否正确声明了mimeType()mimeData()方法,以便在拖动操作期间调用它们。

我一直在文档QT4.6 - Using Model/View Classes

中关注此页面

1 个答案:

答案 0 :(得分:1)

以下是最新文档的链接:

http://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views

我最近研究过一些代码,用于在单个表上进行单元格的内部移动,但在我研究它时,我发现了一些其他有用的链接。

http://qt-project.org/forums/viewthread/14197

我使用了上述链接中的代码并将其翻译为使用QTableView代替QTableWidget

void MyTableView::dropEvent(QDropEvent *event)
{
    // TODO: switch to using EyeTrackerModel::dropMimeData instead
    QPoint old_coordinates = QPoint(-1,-1);
    int dropAction = event->dropAction();
    if(currentIndex().isValid()) //Check if user is not accessing empty cell
    {
        old_coordinates = QPoint( currentIndex().row(), currentIndex().column() );
    }

    QTableView::dropEvent(event);
    qDebug() << "Detected drop event...";
    event->acceptProposedAction();
    if( this->indexAt(event->pos()).isValid() && old_coordinates != QPoint(-1, -1))
    {
        qDebug() << "Drop Event Accepted.";
        qDebug() << "Source: " << old_coordinates.x() << old_coordinates.y()
                 << "Destination: " << this->indexAt(event->pos()).row()
                 << this->indexAt(event->pos()).column()
                 << "Type: " << dropAction;

        emit itemDropped( old_coordinates.x(), old_coordinates.y(),
                          this->indexAt(event->pos()).row(),
                          this->indexAt(event->pos()).column(),
                          dropAction);
    }
    // resize rows and columns right after a move!
    this->doAdjustSize();
}

即使以下答案适用于Python,它在检查我可能缺少的内容时仍然有帮助。

QT: internal drag and drop of rows in QTableView, that changes order of rows in QTableModel

以下是我最近与拖放相关的项目中的一些代码片段。我认为你已经拥有了大部分内容,但为了完整起见我将它们包含在下面。

QAbstractTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)

Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const
{
    Q_UNUSED(index)
//    if(index.isValid())
        return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
//    else
//        return Qt::ItemIsDropEnabled | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QAbstractTableView(QWidget *parent) :
    QTableView(parent)
{
    setAcceptDrops(true);
    setDefaultDropAction(Qt::MoveAction);
}

希望有所帮助。

相关问题