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. [SOLVED] QTreeWidget: Drag and Drop.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTreeWidget: Drag and Drop.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.4k 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.
  • V Offline
    V Offline
    Vovasty
    wrote on last edited by Vovasty
    #1

    Hi all,

    I have main window with QGraphicsScene subclass and QTreeWidget subclass. In QTreeWidget subclass I reimplemented mousePressEvent() and mouseMoveEvent() for adding ability drag and drop operation. In tree widget I add some checkable items and I can add to scene from tree widget graphic item using drag and drop or just set checkbox on the tree widget's item. Everything works fine but I have little bug. After item from tree widget was dropped into the scene, for selecting other item on tree widget I must click two times on this item (I can set checkbox but item still not selected and I must click on item again).
    My code for mousePressEvent() and mouseMoveEvent():

    void MyTreeWidget::mousePressEvent(QMouseEvent *event) {
        QTreeWidget::mousePressEvent(event);
    
        if (event->button() == Qt::LeftButton)
            _dragStartPos = event->pos();
    }
    
    void MyTreeWidget::mouseMoveEvent(QMouseEvent *event) {
        QTreeWidget::mouseMoveEvent(event);
    
        QTreeWidgetItem *item = itemAt(_dragStartPos);
        if (!item)
            return;
    
        if (item->checkState(0) == Qt::Checked)
            return;
    
        for (int i = 0; i < topLevelItemCount(); ++i) {
            if (topLevelItem(i) == item)
                return;
        }
    
        QByteArray itemData;
        itemData.setNum(item->data(0, Qt::UserRole).toInt());
    
        QMimeData *mimeData = new QMimeData;
        mimeData->setData("mydata", itemData);
    
        QDrag *drag = new QDrag(this);
        drag->setMimeData(mimeData);
        drag->exec(Qt::MoveAction);
    }
    

    Thanks for any help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What don't you use the drag and drop functionality already implemented in the item view classes ?

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

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What don't you use the drag and drop functionality already implemented in the item view classes ?

        V Offline
        V Offline
        Vovasty
        wrote on last edited by
        #3

        @SGaist
        You mean use this http://doc.qt.io/qt-4.8/model-view-programming.html#using-drag-and-drop-with-item-views ?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vovasty
          wrote on last edited by
          #4

          I tried to implement startDrag() method instead mouseMoveEvent() and this works fine for main window. But if I call second window (modal) from my main window the drag operation not working for second window.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alex_malyu
            wrote on last edited by
            #5

            Modal window supposed not to be able to interact with other widgets.
            It is the purpose of modality.
            You should change the design to modeless if you need such interaction.

            V 1 Reply Last reply
            0
            • A alex_malyu

              Modal window supposed not to be able to interact with other widgets.
              It is the purpose of modality.
              You should change the design to modeless if you need such interaction.

              V Offline
              V Offline
              Vovasty
              wrote on last edited by
              #6

              @alex_malyu said:

              Modal window supposed not to be able to interact with other widgets.
              It is the purpose of modality.
              You should change the design to modeless if you need such interaction.

              Thanks, but in my case drag operation not working inside modal window when I implemented startDrag() method instead mouseMoveEvent().

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Vovasty Yes, that's the documentation I was thinking about

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

                V 1 Reply Last reply
                0
                • SGaistS SGaist

                  @Vovasty Yes, that's the documentation I was thinking about

                  V Offline
                  V Offline
                  Vovasty
                  wrote on last edited by
                  #8

                  @SGaist

                  Ok, thank you.

                  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