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. Starting Drag'n'Drop in QGraphicsScene::mouseMoveEvent()
Forum Updated to NodeBB v4.3 + New Features

Starting Drag'n'Drop in QGraphicsScene::mouseMoveEvent()

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

    Hello dear community!

    What I try to do
    I am trying to add Drag'n'Drop support to my QGraphicsScene to drag and drop items into other items. Also the normal move-functionality of the items should be retained (flag QGraphicsItem::ItemIsMovable).

    In the mouseMoveEvent() method I can tell if a new QDrag object should be created (by testing for a minimal drag distance etc.).

    Then I call QDrag::exec(), but when the drag operation is finished, items cannot be moved correctly anymore.

    My question:
    Is it okay to call QDrag::exec() from within mouseMoveEvent(), or more precisely between mouseEnterEvent() and mouseReleaseEvent()? Or is that unsupported?

    My guess why this fails
    I noticed that dragEnterEvent(), dragMoveEvent() and dropEvent() are called correctly, but no mouseReleaseEvent() after that anymore. I guess that QGraphicsScene needs for each mousePressEvent() a mouseReleaseEvent() to do some internal cleanup work.
    But after the drag operation started, the mouseReleaseEvent() is not called anymore, so I guess this leads to an invalid state within QGraphicsScene.

    I have prepared a minimal test application which shows the problem. I hope that this is the right place to ask the question? Or should I use the bug tracker instead?

    Thanks a lot for your help and time!
    Greetings Ingo

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      using mouseMoveEvent for starting the drag should be the correct way since QDrag::exec() calls processEvents internally. So all queued events will be send to their receivers.
      May it be that you dont call the base class implementations of the event handlers so the items stay in a wrong state?

      --- 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

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bingo2
        wrote on last edited by
        #3

        Thanks for your answer!

        I think I am calling the base implementation (see my example below), in fact the problem also arises when I do not override mousePressEvent() or mouseReleaseEvent().

        In the example application you can move the rectangles, and while moving, press CTRL to start a drag. When you drop and move the same rectangle it jumps straight back to the position where it was, before the first move.

        What am I doing wrong?

        Example Application:
        @
        #include <QGraphicsRectItem>
        #include <QGraphicsScene>
        #include <QGraphicsView>
        #include <QApplication>
        #include <QGraphicsSceneMouseEvent>
        #include <QMimeData>
        #include <QDrag>
        #include <iostream>

        class Scene : public QGraphicsScene {
        public:
        Scene() {
        QGraphicsRectItem* item;

        // add first item
        item = new QGraphicsRectItem();
        item->setRect(0, 0, 20, 20);
        item->setBrush(Qt::red);
        item->setFlag(QGraphicsItem::ItemIsSelectable);
        item->setFlag(QGraphicsItem::ItemIsMovable);
        item->setPos(40, 40);
        addItem(item);

        // add second item
        item = new QGraphicsRectItem();
        item->setRect(0, 0, 20, 20);
        item->setBrush(Qt::blue);
        item->setFlag(QGraphicsItem::ItemIsSelectable);
        item->setFlag(QGraphicsItem::ItemIsMovable);
        item->setPos(20, 60);
        addItem(item);
        }
        virtual ~Scene() {
        }
        virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
        QGraphicsScene::mouseMoveEvent(event);

         if (event->buttons() != 0 && event->modifiers() != 0) {
          // perform drag
          QMimeData* mimeData = new QMimeData();
          QDrag* drag = new QDrag(event->widget());
          drag->setMimeData(mimeData);
          drag->exec&#40;&#41;;
         }
        }
        
        
        virtual void mousePressEvent(QGraphicsSceneMouseEvent *event&#41; {
         QGraphicsScene::mousePressEvent(event&#41;;
         std::cout << "mousePressed" << std::endl;
        }
        
        virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event&#41; {
         QGraphicsScene::mouseReleaseEvent(event);
         std::cout << "mouseRelease" << std::endl;
        }
        

        };

        int main(int argc, char* argv[]) {
        QApplication app(argc, argv);

        Scene* scene = new Scene();

        QGraphicsView* view = new QGraphicsView(scene);
        view->setAcceptDrops(true);
        view->setDragMode(QGraphicsView::RubberBandDrag);
        view->setAlignment(Qt::AlignCenter);
        view->setMouseTracking(true);
        view->show();

        return app.exec();
        }
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bingo2
          wrote on last edited by
          #4

          I created bug report https://bugreports.qt-project.org/browse/QTBUG-33146 for this.

          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