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. QGraphicsSceneMouseEvent not working
QtWS25 Last Chance

QGraphicsSceneMouseEvent not working

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 311 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.
  • H Offline
    H Offline
    herrgross
    wrote on last edited by
    #1

    Hi everybody,

    I'm trying to move an object in QGraphicsScene with the mouse.
    Cannot find out why it's not working.
    Here's what I'm doing:

    Simple GraphicScene with some Items in a view:

    setGeometry(1400, 500, 500, 500);
    
    scene = new QGraphicsScene(0, 0, 400, 400);
    
    view = new QGraphicsView(scene, this);
    view->setGeometry(QRect(5, 30, 400, 400));
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
    QPen receiverPen(qRgb(80, 255, 47));
    QPen actorPen(Qt::red);
    QPen linePen(Qt::black);
    linePen.setWidth(2);
    
    QBrush receiverBrush(qRgb(80, 255, 47));
    QBrush actorBrush(Qt::red);
    
    receiver1 = scene->addEllipse(-10, -10, 20, 20, receiverPen, receiverBrush);
    receiver2 = scene->addEllipse(scene->width() - 10, -10, 20, 20, receiverPen, receiverBrush);
    
    actor = scene->addEllipse(scene->width()/2 - 5, scene->height()/2 - 5, 10, 10, actorPen, actorBrush);
    

    And then the method that shoud enable me to move the actor-Item:

    void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        actor->setPos(event->pos());
        statusBar()->showMessage(QString::number(event->pos().x()));    
    }
    

    I added the statusbarmessage to see, if the event is arriving at all. It seems not to.
    When I try with a QMouseEvent it does, but only outside the QGraphicsView...

    Would be very happy, if somebody could tell me what I'm missing...

    Thanks, Michael

    Pl45m4P 1 Reply Last reply
    0
    • H herrgross

      @Pl45m4 thank you, with the Flag, that's working, I already had that. But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #5

      @herrgross said in QGraphicsSceneMouseEvent not working:

      But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

      If you subclass either the item or the scene, you can react on position changes or read the new item position.
      Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      H 1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        check the example out
        https://doc.qt.io/qt-6/qtwidgets-graphicsview-elasticnodes-example.html

        1 Reply Last reply
        0
        • H herrgross

          Hi everybody,

          I'm trying to move an object in QGraphicsScene with the mouse.
          Cannot find out why it's not working.
          Here's what I'm doing:

          Simple GraphicScene with some Items in a view:

          setGeometry(1400, 500, 500, 500);
          
          scene = new QGraphicsScene(0, 0, 400, 400);
          
          view = new QGraphicsView(scene, this);
          view->setGeometry(QRect(5, 30, 400, 400));
          view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          
          QPen receiverPen(qRgb(80, 255, 47));
          QPen actorPen(Qt::red);
          QPen linePen(Qt::black);
          linePen.setWidth(2);
          
          QBrush receiverBrush(qRgb(80, 255, 47));
          QBrush actorBrush(Qt::red);
          
          receiver1 = scene->addEllipse(-10, -10, 20, 20, receiverPen, receiverBrush);
          receiver2 = scene->addEllipse(scene->width() - 10, -10, 20, 20, receiverPen, receiverBrush);
          
          actor = scene->addEllipse(scene->width()/2 - 5, scene->height()/2 - 5, 10, 10, actorPen, actorBrush);
          

          And then the method that shoud enable me to move the actor-Item:

          void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
          {
              actor->setPos(event->pos());
              statusBar()->showMessage(QString::number(event->pos().x()));    
          }
          

          I added the statusbarmessage to see, if the event is arriving at all. It seems not to.
          When I try with a QMouseEvent it does, but only outside the QGraphicsView...

          Would be very happy, if somebody could tell me what I'm missing...

          Thanks, Michael

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #3

          @herrgross said in QGraphicsSceneMouseEvent not working:

          void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
          

          This is already not correct.
          MainWindow does not receive QGraphicsSceneMouseEvents.

          I'm trying to move an object in QGraphicsScene with the mouse.

          actor->setFlag(QGraphicsItem::ItemIsMovable);
          

          Done.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          H 1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @herrgross said in QGraphicsSceneMouseEvent not working:

            void MainWindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
            

            This is already not correct.
            MainWindow does not receive QGraphicsSceneMouseEvents.

            I'm trying to move an object in QGraphicsScene with the mouse.

            actor->setFlag(QGraphicsItem::ItemIsMovable);
            

            Done.

            H Offline
            H Offline
            herrgross
            wrote on last edited by herrgross
            #4

            @Pl45m4 thank you, with the Flag, that's working, I already had that. But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

            Pl45m4P 1 Reply Last reply
            0
            • H herrgross

              @Pl45m4 thank you, with the Flag, that's working, I already had that. But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #5

              @herrgross said in QGraphicsSceneMouseEvent not working:

              But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

              If you subclass either the item or the scene, you can react on position changes or read the new item position.
              Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              H 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @herrgross said in QGraphicsSceneMouseEvent not working:

                But I had no access to the position data, that's why I tried with mouseMoveEvent. Now I am working around with the elasticnodes-example, that @JoeCFD recommanded, that seems to work, but is a lot more work...

                If you subclass either the item or the scene, you can react on position changes or read the new item position.
                Using the base classes only, things are tough. It's weird anyway, since as soon as you want custom behavior, it would end there without any re-implementations or subclassing.

                H Offline
                H Offline
                herrgross
                wrote on last edited by
                #6

                @Pl45m4 yes, that's how the nodes-example works: subclassing Items and Scene. So I'm modifying that for my needs and learn on the way what I can...

                Pl45m4P 1 Reply Last reply
                0
                • H herrgross

                  @Pl45m4 yes, that's how the nodes-example works: subclassing Items and Scene. So I'm modifying that for my needs and learn on the way what I can...

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #7

                  @herrgross said in QGraphicsSceneMouseEvent not working:

                  yes, that's how the nodes-example works: subclassing Items and Scene

                  In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.
                  Since Qt is a C++ framework, it is made (and designed) to be used this way. The programmer (Qt user) uses Qt to develop software for the "enduser" (or for education, or whatever purpose).


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  H 1 Reply Last reply
                  2
                  • Pl45m4P Pl45m4

                    @herrgross said in QGraphicsSceneMouseEvent not working:

                    yes, that's how the nodes-example works: subclassing Items and Scene

                    In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.
                    Since Qt is a C++ framework, it is made (and designed) to be used this way. The programmer (Qt user) uses Qt to develop software for the "enduser" (or for education, or whatever purpose).

                    H Offline
                    H Offline
                    herrgross
                    wrote on last edited by
                    #8

                    @Pl45m4 said in QGraphicsSceneMouseEvent not working:

                    In 99.9% of the cases, the first thing you do is subclassing the (base-)class of your interest to gain "full control" over it and be able to implement virtual functions, add your own functions / logic or re-implement event handlers for mouse event, for example.

                    @Pl45m4 I just finished some education in C++, but this was hardly a topic. So I am very grateful for this kind of advice - thank's a lot

                    1 Reply Last reply
                    0
                    • H herrgross has marked this topic as solved on

                    • Login

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