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

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 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);
        }
    }
    JonBJ Pl45m4P 2 Replies Last reply
    0
    • C CAP 33

      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);
          }
      }
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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 JonBJ 2 Replies Last reply
      0
      • JonBJ JonB

        @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 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
        • JonBJ JonB

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          0
          • JonBJ JonB

            @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 last edited by
            #5

            @JonB
            No

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

                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);
                    }
                }
                Pl45m4P Online
                Pl45m4P Online
                Pl45m4
                wrote on 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
                0
                • Pl45m4P Pl45m4

                  @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 last edited by
                  #8

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

                  SGaistS 1 Reply Last reply
                  0
                  • C CAP 33

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

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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 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

                      • Login

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