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. Drag & drop in QTreeWidget
QtWS25 Last Chance

Drag & drop in QTreeWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
treewidgettreeviewtreemodeldrag&drop
7 Posts 3 Posters 11.1k 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.
  • M Offline
    M Offline
    Maluna34
    wrote on last edited by Maluna34
    #1

    Hello ! :)

    (Sorry for my english)

    I am currently trying to make a drag & drop in a QTreeWidget. So I put the corresponding settings and the method dropEvent :

    class TreeWidget : public QTreeWidget
    {
    protected:
    
      virtual void dropEvent(QDropEvent *event) override
      {
        QModelIndex index = indexAt(event->pos());
        if (!index.isValid()) {  // just in case
          event->setDropAction(Qt::IgnoreAction);
          return;
        }
    
        QTreeWidgetItem* item = itemFromIndex(index);
        qDebug() << "drop on item" << item->text(0);
    
        QTreeWidget::dropEvent(event);
      }
    };
    
    int main()
    {
      TreeWidget *listWidget = new TreeWidget;
      listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
      listWidget->setDragEnabled(true);
      listWidget->viewport()->setAcceptDrops(true);
      listWidget->setDropIndicatorShown(true);
      listWidget->setDragDropMode(QAbstractItemView::InternalMove);
    }
    

    But in my case, I would like to move only parent items. In the code, I get the destination item, but how get dragged item ?

    Have I to overload a drag method ? Launch the drag myself from mousePressEvent ? What is the best way to do ?

    Thanks !

    jsulmJ 1 Reply Last reply
    0
    • M Maluna34

      Hello ! :)

      (Sorry for my english)

      I am currently trying to make a drag & drop in a QTreeWidget. So I put the corresponding settings and the method dropEvent :

      class TreeWidget : public QTreeWidget
      {
      protected:
      
        virtual void dropEvent(QDropEvent *event) override
        {
          QModelIndex index = indexAt(event->pos());
          if (!index.isValid()) {  // just in case
            event->setDropAction(Qt::IgnoreAction);
            return;
          }
      
          QTreeWidgetItem* item = itemFromIndex(index);
          qDebug() << "drop on item" << item->text(0);
      
          QTreeWidget::dropEvent(event);
        }
      };
      
      int main()
      {
        TreeWidget *listWidget = new TreeWidget;
        listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
        listWidget->setDragEnabled(true);
        listWidget->viewport()->setAcceptDrops(true);
        listWidget->setDropIndicatorShown(true);
        listWidget->setDragDropMode(QAbstractItemView::InternalMove);
      }
      

      But in my case, I would like to move only parent items. In the code, I get the destination item, but how get dragged item ?

      Have I to overload a drag method ? Launch the drag myself from mousePressEvent ? What is the best way to do ?

      Thanks !

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Maluna34 http://doc.qt.io/qt-5/qdropevent.html#source should be what you're looking for.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Maluna34 http://doc.qt.io/qt-5/qdropevent.html#source should be what you're looking for.

        M Offline
        M Offline
        Maluna34
        wrote on last edited by
        #3

        @jsulm Thank you but I can't cast QObject to QTreeWidgetItem. :/

        jsulmJ 1 Reply Last reply
        0
        • M Maluna34

          @jsulm Thank you but I can't cast QObject to QTreeWidgetItem. :/

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Maluna34 You can (if it is a QTreeWidgetItem). And it is QObject* not QObject.
          Take a look at http://doc.qt.io/qt-5/qobject.html#qobject_cast

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          raven-worxR 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Maluna34 You can (if it is a QTreeWidgetItem). And it is QObject* not QObject.
            Take a look at http://doc.qt.io/qt-5/qobject.html#qobject_cast

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @jsulm
            source() only returns a widget of the same application as the docs clearly say.

            The QTreeWidgetItem data is "encoded" in the drop-data (QMimeData) itself.
            See source of QStandardItemModel::dropMimeData() and QStandardItemModelPrivate::decodeDataRecursive()
            So unfortunately this is not accessible via public API.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            M 1 Reply Last reply
            2
            • raven-worxR raven-worx

              @jsulm
              source() only returns a widget of the same application as the docs clearly say.

              The QTreeWidgetItem data is "encoded" in the drop-data (QMimeData) itself.
              See source of QStandardItemModel::dropMimeData() and QStandardItemModelPrivate::decodeDataRecursive()
              So unfortunately this is not accessible via public API.

              M Offline
              M Offline
              Maluna34
              wrote on last edited by Maluna34
              #6

              @raven-worx Thank you ! Indeed, I manage to get the item text with this code :

              virtual void dropEvent(QDropEvent *event) override
              {
                      QString format = event->mimeData()->formats().at(0);
                      QByteArray data = event->mimeData()->data(format);
                      QDataStream stream(&data, QIODevice::ReadOnly);
              
                      int r, c;
                      QStandardItem *sitem = new QStandardItem;
                      stream >> r >> c >> *sitem;
              
                      qDebug() << r << "-" << c << sitem->text();
              }
              

              I think it's correct. Maybe it's also possible to get either the depth of the element or get the right type (QTreeWidgetItem).

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Maluna34
                wrote on last edited by
                #7

                The best way seems to use QDropEvent::source to get the QTreeWidget and use currentItem() on it. :)

                1 Reply Last reply
                0

                • Login

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