Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    2
    4
    189
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Rodrigo B. last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • R
        Rodrigo B. last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 0
          • R
            Rodrigo B. last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • R
              Rodrigo B. last edited by

              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.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post