Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Attempt to add Drag and Drop to Editable Tree Model example not working
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 404 Views
  • 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 Offline
    R Offline
    Rodrigo B.
    wrote on last edited by
    #1

    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
    0
    • R Offline
      R Offline
      Rodrigo B.
      wrote on last edited by
      #4

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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
        0
        • R Offline
          R Offline
          Rodrigo B.
          wrote on last edited by
          #3

          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
          0
          • R Offline
            R Offline
            Rodrigo B.
            wrote on last edited by
            #4

            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
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved