【问题标题】:Dragging from an inherited QTableView从继承的 QTableView 中拖动
【发布时间】:2015-01-19 17:03:11
【问题描述】:

我的应用程序中有一个名为“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 - 我得到了适当的光标,甚至在 treeview 上触发了一个 dropMimeData 事件。

但是dropMimeData()方法中的行值和列值总是-1,“partsview”中的两个方法都没有被调用。

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

我一直在关注文档QT4.6 - Using Model/View Classes中的这个页面

【问题讨论】:

    标签: qt


    【解决方案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

    这是我最近项目中与拖放相关的一些代码 sn-ps。我认为您已经拥有其中的大部分,但为了完整起见,我将它们包括在下面。

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

    希望对您有所帮助。

    【讨论】:

    • 感谢您的完整回答,phyatt。但是,它并没有回答我的问题 - 无论如何也不是直接的。我更多地停留在事情的开始而不是下降。虽然我对 Qt 很陌生,但我可能找错地方了。
    • 所以有很多关于支持拖放的内容。我在这里找到了一个很好的例子:doc.qt.io/qt-5/qtwidgets-itemviews-puzzle-example.html 尝试匹配他们包含在您的模型中的所有拖放元素,或者将他们的模型修改为您的模型。您应该可以在Qt Creator &gt; Welcome &gt; Examples 中找到示例。
    • 谢谢,phyatt - 我已经看过那个例子了,我再看看。
    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2023-03-27
    相关资源
    最近更新 更多