Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Attempt to add Drag and Drop to Editable Tree Model example not working



  • Hello,

    I'm trying to learn how to implement Drag and Drop to model/view settings.

    As an exercise, I attempted to do that to the Editable Tree Model example available at the Qt web site.

    To try to implement Drag and Drop, I followed the instructions in Qt's documentation on "Using Drag and Drop with View Items", more specifically "Using model/view classes".

    I placed the code for my attempt at a GitHub repository. This is the commit containing the relevant modifications according to the documentation.

    However, this didn't quite work. While I can drag and drop items, the copied item appears as blank. This can be seen in this screen capture.

    Note that the documentation describes the need to re-implement QAbstractItemModel's dropMimeData for drag and drop functionality, which I did not. This is because, upon inspecting the source code for that class, I find that its default implementation should already work in copying items in drag and drop, since it uses the default application/x-qabstractitemmodeldatalist MIME format and uses setItemData for the inserted items.

    What is missing in the code for this to work properly?

    Thank you.



  • I've posted this question on Stack Overflow and got the answer:

    The model provided by the example only accepts setting data with role Qt::EditRole. See line 263 in treemodel.cpp

    bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (role != Qt::EditRole)
            return false;
        ...
    }
    

    Removing that part or adding Qt::DisplayRole and the data will be set properly.


  • Lifetime Qt Champion

    Hi,

    Why not use internal move for your drag and drop action ?



  • Hi @SGaist,

    My reasoning is that I wanted to leave it open to a future extension where external drops could occur.

    In any case, I just tried your suggestion. I changed the Drag and Drop mode to InternalMove but observe the exact same behavior of blank nodes being created.



  • I've posted this question on Stack Overflow and got the answer:

    The model provided by the example only accepts setting data with role Qt::EditRole. See line 263 in treemodel.cpp

    bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (role != Qt::EditRole)
            return false;
        ...
    }
    

    Removing that part or adding Qt::DisplayRole and the data will be set properly.


Log in to reply