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. Question about QGraphicsScene and QGraphicsPixmapItem
QtWS25 Last Chance

Question about QGraphicsScene and QGraphicsPixmapItem

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicspixmapqgraphicsscenemousepresseventmousemoveevent
10 Posts 4 Posters 1.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.
  • C Offline
    C Offline
    CAP 33
    wrote on 24 Dec 2021, 10:22 last edited by
    #1

    I want to do the following: when I click on the scene, an object Server is added to it at the coordinates of the mouse click and that object I can move.
    In an object inherited from QGraphicsPixmapItem, defined mouseMoveEvent methods to move it. Everything is working. When I click on an object and move it with the mouse, the object also moves.
    Next, I created a new class inherited from QGraphicsScene and defined the mousePressEvent method in it, which emits a click signal that takes the coordinates of the scene. In the main program, I implemented a slot that adds an object to the scene at the coordinates of the event. Connected signal and slot.
    As a result of testing, it turned out that the object is added to the scene when you click on the scene. But it moves only when you double-click.
    How do I make it move on one click?

    class Server : public QGraphicsPixmapItem{
    public:
        Server(QPoint);
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    };
    void Server::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
    
        this->QGraphicsPixmapItem::setPos(QGraphicsPixmapItem::mapToScene(event->pos()));
    
    class Field : public QGraphicsScene
    {
        Q_OBJECT
    public:
        Field();
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
    
    signals:
        void Press(QPoint);
    };
    void Field::mousePressEvent(QGraphicsSceneMouseEvent *event){
        emit Press((event->scenePos().toPoint()));
    }
    
    scene = new Field;
    ui->graphicsView->setScene(scene);
    connect(scene, SIGNAL(Press(QPoint)), this, SLOT(press_on_scene(QPoint)));
    
    void MainWindow::press_on_scene(QPoint coordinate){
        if(ui->pushButton_2->isChecked()){
            Server *serv = new Server(coordinate);
            scene->addItem(serv);
        }
    }
    J P 2 Replies Last reply 24 Dec 2021, 11:31
    0
    • C CAP 33
      24 Dec 2021, 10:22

      I want to do the following: when I click on the scene, an object Server is added to it at the coordinates of the mouse click and that object I can move.
      In an object inherited from QGraphicsPixmapItem, defined mouseMoveEvent methods to move it. Everything is working. When I click on an object and move it with the mouse, the object also moves.
      Next, I created a new class inherited from QGraphicsScene and defined the mousePressEvent method in it, which emits a click signal that takes the coordinates of the scene. In the main program, I implemented a slot that adds an object to the scene at the coordinates of the event. Connected signal and slot.
      As a result of testing, it turned out that the object is added to the scene when you click on the scene. But it moves only when you double-click.
      How do I make it move on one click?

      class Server : public QGraphicsPixmapItem{
      public:
          Server(QPoint);
          void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
      };
      void Server::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
      {
      
          this->QGraphicsPixmapItem::setPos(QGraphicsPixmapItem::mapToScene(event->pos()));
      
      class Field : public QGraphicsScene
      {
          Q_OBJECT
      public:
          Field();
          void mousePressEvent(QGraphicsSceneMouseEvent *event);
      
      signals:
          void Press(QPoint);
      };
      void Field::mousePressEvent(QGraphicsSceneMouseEvent *event){
          emit Press((event->scenePos().toPoint()));
      }
      
      scene = new Field;
      ui->graphicsView->setScene(scene);
      connect(scene, SIGNAL(Press(QPoint)), this, SLOT(press_on_scene(QPoint)));
      
      void MainWindow::press_on_scene(QPoint coordinate){
          if(ui->pushButton_2->isChecked()){
              Server *serv = new Server(coordinate);
              scene->addItem(serv);
          }
      }
      J Offline
      J Offline
      JonB
      wrote on 24 Dec 2021, 11:31 last edited by
      #2

      @CAP-33
      I don't quite follow what you are saying with "But it moves only when you double-click." (I never seem to, unlike some responders here who are clairvoyant!). Are you saying it worked as you wanted to before you created a derived class? [Also your Server::mouseMoveEvent() method above is "chopped off".]

      Anyway, in your mouseMoveEvent() overrides you do not call the base implementation. I always do that. Is that your problem?

      C J 2 Replies Last reply 24 Dec 2021, 12:17
      0
      • J JonB
        24 Dec 2021, 11:31

        @CAP-33
        I don't quite follow what you are saying with "But it moves only when you double-click." (I never seem to, unlike some responders here who are clairvoyant!). Are you saying it worked as you wanted to before you created a derived class? [Also your Server::mouseMoveEvent() method above is "chopped off".]

        Anyway, in your mouseMoveEvent() overrides you do not call the base implementation. I always do that. Is that your problem?

        C Offline
        C Offline
        CAP 33
        wrote on 24 Dec 2021, 12:17 last edited by CAP 33
        #3

        @JonB
        At first the scene was an object of the QGraphicsScene class, the QGraphicsScene::mousePressEvent method was not implemented and the Server moved around the scene with a single mouse click.
        After I replaced the scene with an object of the Field class, in which I implemented the Field::mousePressEvent method, the Server can still move around the scene, but for some reason not with one mouse click, but with a double click.

        1 Reply Last reply
        0
        • J JonB
          24 Dec 2021, 11:31

          @CAP-33
          I don't quite follow what you are saying with "But it moves only when you double-click." (I never seem to, unlike some responders here who are clairvoyant!). Are you saying it worked as you wanted to before you created a derived class? [Also your Server::mouseMoveEvent() method above is "chopped off".]

          Anyway, in your mouseMoveEvent() overrides you do not call the base implementation. I always do that. Is that your problem?

          J Offline
          J Offline
          JonB
          wrote on 24 Dec 2021, 13:35 last edited by
          #4

          @JonB said in Question about QGraphicsScene and QGraphicsPixmapItem:

          Anyway, in your mouseMoveEvent() overrides you do not call the base implementation. I always do that. Is that your problem?

          C 1 Reply Last reply 24 Dec 2021, 14:52
          0
          • J JonB
            24 Dec 2021, 13:35

            @JonB said in Question about QGraphicsScene and QGraphicsPixmapItem:

            Anyway, in your mouseMoveEvent() overrides you do not call the base implementation. I always do that. Is that your problem?

            C Offline
            C Offline
            CAP 33
            wrote on 24 Dec 2021, 14:52 last edited by
            #5

            @JonB
            No

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 Dec 2021, 20:24 last edited by
              #6

              Hi,

              Why not make your item movable ? That way you don't have to do any special handling.

              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
              1
              • C CAP 33
                24 Dec 2021, 10:22

                I want to do the following: when I click on the scene, an object Server is added to it at the coordinates of the mouse click and that object I can move.
                In an object inherited from QGraphicsPixmapItem, defined mouseMoveEvent methods to move it. Everything is working. When I click on an object and move it with the mouse, the object also moves.
                Next, I created a new class inherited from QGraphicsScene and defined the mousePressEvent method in it, which emits a click signal that takes the coordinates of the scene. In the main program, I implemented a slot that adds an object to the scene at the coordinates of the event. Connected signal and slot.
                As a result of testing, it turned out that the object is added to the scene when you click on the scene. But it moves only when you double-click.
                How do I make it move on one click?

                class Server : public QGraphicsPixmapItem{
                public:
                    Server(QPoint);
                    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
                };
                void Server::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
                {
                
                    this->QGraphicsPixmapItem::setPos(QGraphicsPixmapItem::mapToScene(event->pos()));
                
                class Field : public QGraphicsScene
                {
                    Q_OBJECT
                public:
                    Field();
                    void mousePressEvent(QGraphicsSceneMouseEvent *event);
                
                signals:
                    void Press(QPoint);
                };
                void Field::mousePressEvent(QGraphicsSceneMouseEvent *event){
                    emit Press((event->scenePos().toPoint()));
                }
                
                scene = new Field;
                ui->graphicsView->setScene(scene);
                connect(scene, SIGNAL(Press(QPoint)), this, SLOT(press_on_scene(QPoint)));
                
                void MainWindow::press_on_scene(QPoint coordinate){
                    if(ui->pushButton_2->isChecked()){
                        Server *serv = new Server(coordinate);
                        scene->addItem(serv);
                    }
                }
                P Offline
                P Offline
                Pl45m4
                wrote on 25 Dec 2021, 03:37 last edited by
                #7

                @CAP-33 said in Question about QGraphicsScene and QGraphicsPixmapItem:

                But it moves only when you double-click.

                Could be a focus issue. First click sets focus from scene to item, second activates the click and mouseMovement of your item.
                But I would do what @SGaist suggested. If you make your item movable, you shouldn't face any problems


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

                ~E. W. Dijkstra

                C 1 Reply Last reply 25 Dec 2021, 09:32
                0
                • P Pl45m4
                  25 Dec 2021, 03:37

                  @CAP-33 said in Question about QGraphicsScene and QGraphicsPixmapItem:

                  But it moves only when you double-click.

                  Could be a focus issue. First click sets focus from scene to item, second activates the click and mouseMovement of your item.
                  But I would do what @SGaist suggested. If you make your item movable, you shouldn't face any problems

                  C Offline
                  C Offline
                  CAP 33
                  wrote on 25 Dec 2021, 09:32 last edited by
                  #8

                  @Pl45m4
                  I have set the flag QGraphicsPixmapItem::setFlag(QGraphicsPixmapItem::ItemIsMovable, true), but it didn’t solve the problem.

                  S 1 Reply Last reply 25 Dec 2021, 20:48
                  0
                  • C CAP 33
                    25 Dec 2021, 09:32

                    @Pl45m4
                    I have set the flag QGraphicsPixmapItem::setFlag(QGraphicsPixmapItem::ItemIsMovable, true), but it didn’t solve the problem.

                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Dec 2021, 20:48 last edited by
                    #9

                    @CAP-33 did you remove your custom mouse handling ?

                    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
                    1
                    • C Offline
                      C Offline
                      CAP 33
                      wrote on 26 Dec 2021, 09:44 last edited by
                      #10

                      I solved my problem via event filter of scene.

                      ui->graphicsView->scene()->installEventFilter(this);
                      
                      bool MainWindow::eventFilter(QObject* obj, QEvent* event){
                          if(event->type() == QEvent::GraphicsSceneMousePress){
                              if(ui->addServerBtn->isChecked()){
                                  QGraphicsSceneMouseEvent* mouseSceneEvent;
                                  mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
                                  Server *serv = new Server(mouseSceneEvent->scenePos().toPoint());
                                  scene->addItem((QGraphicsPixmapItem*)serv);
                              }
                          }
                      return false;
                      }
                      
                      1 Reply Last reply
                      0

                      1/10

                      24 Dec 2021, 10:22

                      • Login

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