简体   繁体   English

Qt4:从QAbstractItemModel读取默认的mimeData

[英]Qt4: Read Default mimeData from QAbstractItemModel

What I want to do is very similar to this . 我想做的事情与非常相似。 Except that I am working with a QAbstractItemModel that has a tree structure and am interested in more than just the row and column. 除了我正在使用具有树结构的QAbstractItemModel之外,他对行和列的兴趣不止于此。 In fact, in my model, column is always 0. But in order to implement drag and drop, I need to get the parent, children, and the opaque pointer that internalPointer() returns. 实际上,在我的模型中,列始终为0。但是,为了实现拖放操作,我需要获取parent,children和internalPointer()返回的不透明指针。 Here is some relevant code. 这是一些相关的代码。 CTreeView extends QTreeView. CTreeView扩展了QTreeView。

void CTreeView::dragEnterEvent(QDragEnterEvent* event)
{
    if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
    {
        event->acceptProposedAction();
    }
}

void CTreeView::dropEvent(QDropEvent* event)
{
    const QMimeData* mime_data = event->mimeData();
    QByteArray encoded_data =
        mime_data->data("application/x-qabstractitemmodeldatalist");
    QDataStream stream(&encoded_data, QIODevice::ReadOnly);
    while (!stream.atEnd())
    {
        // I can do this.
        int row, column;
        stream >> row >> column;
        // But how do I construct the QModelIndex to get the parent, children,
        // and opaque pointer?

        // I have seen other advice that mentions doing this.
        QMap<int, QVariant> role_data_map;
        stream >> row >> col >> role_data_map;

        // Which allows you to do this.
        QList<int> keys = role_data_map.keys();
        BOOST_FOREACH(int key, keys)
        {
            QVariant variant = role_data_map[key];
            // use the variant
        }
        // But that only gets me part of the way there.
    }
}

Any ideas? 有任何想法吗? I only want to support drag and drop within the tree view so I'm thinking about storing the QModelIndexList of selectedIndexes() in a member variable of my subclass and just manipulating it directly in dropEvent(). 我只想在树状视图中支持拖放操作,因此我正在考虑将selectedIndexes()的QModelIndexList存储在子类的成员变量中,然后直接在dropEvent()中对其进行操作。 That seems like cheating somehow so I'm still interested in the Qt way. 这似乎有点作弊,所以我仍然对Qt方式感兴趣。 Please let me know what you think of this idea. 请让我知道您对这个想法的看法。

First it looks like from your code that you are doing dnd the wrong way: you should not overload dropEvent in your view, but instead dropMimeData in your model. 首先,从代码中看来,您正在以错误的方式执行操作:您不应在视图中重载dropEvent,而应在模型中重载dropMimeData。 The following document explains how to do dnd with the model/view framework of Qt: 以下文档说明了如何使用Qt的模型/视图框架进行操作:

http://doc.trolltech.com/latest/model-view-dnd.html http://doc.trolltech.com/latest/model-view-dnd.html

As for your specific problem, which is to access the internalPointer() of the dropped items. 至于您的特定问题,那就是访问已放置项目的internalPointer()。 Storing the indexes in an index of your class is dangerous and error-prone. 将索引存储在类的索引中既危险又容易出错。 What you want to do is store the information you need in the mime data. 您要做的就是将所需的信息存储在mime数据中。 I don't know what your use case is so I cannot guess what this useful data is - but if you just need the value of internalPointer (and can make sure this value will still be valid when the drop event is received), you can just store it, as you decide the format. 我不知道您的用例是什么,所以我无法猜测这个有用的数据是什么-但是,如果您只需要internalPointer的值(并且可以确保在接收到drop事件时该值仍然有效),则可以只需存储它,即可决定格式。 For instance, if your data is referenced by a unique id somewhere (like the row id in a database), you can store this information and have a custom index(int rowid) method in your model that constructs a QModelIndex from this information. 例如,如果您的数据被某个地方的唯一ID引用(例如数据库中的行ID),则可以存储此信息,并在模型中具有自定义index(int rowid)方法,该方法根据此信息构造QModelIndex。 Normally the internalPointer of an index is set during its creation, so this would allow to fetch all the needed information. 通常,索引的internalPointer是在创建索引的过程中设置的,因此可以获取所有需要的信息。

If you tell us how you create your indexes maybe we can help further. 如果您告诉我们如何创建索引,也许我们可以提供进一步的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM