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. How to implement simple internal drag&drop for reordering items in QListView using a custom model
Forum Updated to NodeBB v4.3 + New Features

How to implement simple internal drag&drop for reordering items in QListView using a custom model

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.8k Views 2 Watching
  • 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.
  • Y Offline
    Y Offline
    Youda008
    wrote on 29 Jun 2019, 16:30 last edited by
    #1

    I have a QList of custom structs and i'm using custom model class (subclass of QAbstractListModel) to display those structs in 1-dimensional QListView. I have overriden the methods rowCount, flags and data to construct a display string from the struct elements.

    Now i would like to enable internal drag&drop to be able to reorder the items in the list by dragging them and dropping them between some other items, but this task seems unbeliavably complicated. What exactly do i need to override and what parameters do i need to set? I tried a lot of things, i tried

    view->setDragEnabled( true );
    view->setAcceptDrops( true );
    view->setDragDropMode( QAbstractItemView::InternalMove );
    view->setDefaultDropAction( Qt::MoveAction );
    

    I tried

    Qt::DropActions supportedDropActions() const override {
        return Qt::MoveAction;
    }
    Qt::ItemFlags flags( const QModelIndex & index ) const override{
        return QAbstractItemModel::flags( index ) | Qt::ItemIsDragEnabled;
    }
    

    I tried implementing insertRows and removeRows, but it still doesn't work.

    I haven't found a single example of a code doing exactly that. The official documentation goes very deeply into how view/model pattern works and how to make drag&drops from external apps or from other widgets, but i don't want any of that. I only want simple internal drag&drop for manual reordering of the items in that one list view and nothing more.

    Can someone please help me? Or i'll get nuts from this.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Jun 2019, 17:13 last edited by
      #2

      Hi
      When the build in drag & drop is not enough, then for the model you need to override
      mimeType()
      mimeData()
      dropMimeData()

      example.
      https://www.qtcentre.org/threads/51679-How-to-Drag-and-Drop-working-with-tableview

      ps. Qt Models and Views will drive you slightly nuts but really worth it :)

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        Youda008
        wrote on 29 Jun 2019, 17:57 last edited by
        #3

        But this
        https://doc.qt.io/qt-5/model-view-programming.html#accepting-dropped-data
        says QAbstractListModel has default implementations of mimeType, mimeData, dropMimeData, that should do everything i need.

        M 1 Reply Last reply 29 Jun 2019, 18:23
        0
        • Y Youda008
          29 Jun 2019, 17:57

          But this
          https://doc.qt.io/qt-5/model-view-programming.html#accepting-dropped-data
          says QAbstractListModel has default implementations of mimeType, mimeData, dropMimeData, that should do everything i need.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Jun 2019, 18:23 last edited by
          #4

          @Youda008
          hi
          Yes, you are right, but last time i fiddled with view/custom model DD, it would only accept copy operations and that didn't not work so well with internal reordering. ;)
          I could be wrong though. Maybe we get lucky and @VRonin comes around and tell the painful truth :)

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            Youda008
            wrote on 30 Jun 2019, 18:52 last edited by
            #5

            I got quite a good answer here https://stackoverflow.com/questions/56819085/qt-how-to-implement-simple-internal-dragdrop-for-reordering-items-in-qlistview
            Additionally i discovered few other important things about how it works, which i included in my own answer.
            Not gonna copy&paste here. TL,DR: default mimeData and dropMimeData work only if your underlying data structure only contains elements serialized inside data and deserialized in setData on EditRole. If there is something more in your structs than those display elements, you need your own mimeData and dropMimeData to move the whole content of your struct.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 Jun 2019, 18:56 last edited by
              #6

              Hi,

              Just in case, there's a whole chapter dedicated to that in QT's documentation.

              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
              3

              1/6

              29 Jun 2019, 16:30

              • Login

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