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. Detecting left mouse click

Detecting left mouse click

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 642 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.
  • privatepepperP Offline
    privatepepperP Offline
    privatepepper
    wrote on last edited by
    #1

    Hello,
    How could I detect a left mouse click on the QGraphicsRectItems and know which QGraphicsRectItem was clicked? I am implementing multiple shortest path algorithms and I visualize them and I want that user could draw "walls".

    Pl45m4P 1 Reply Last reply
    0
    • privatepepperP privatepepper

      Hello,
      How could I detect a left mouse click on the QGraphicsRectItems and know which QGraphicsRectItem was clicked? I am implementing multiple shortest path algorithms and I visualize them and I want that user could draw "walls".

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

      @privatepepper

      There are several posts on how to do this. Use the search :)

      https://doc.qt.io/qt-5/qgraphicsitem.html#mousePressEvent


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

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • privatepepperP Offline
        privatepepperP Offline
        privatepepper
        wrote on last edited by privatepepper
        #3

        I added this line to mainwindow header file

        protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent);
        

        And now I am getting a warning - 'MainWindow::mousePressEvenet' hides overloaded virtual function. So, that means that I need to create a separate class for my QGraphicsScene that has QGraphicsRectItems?

        Pl45m4P 1 Reply Last reply
        0
        • privatepepperP privatepepper

          I added this line to mainwindow header file

          protected:
          void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent);
          

          And now I am getting a warning - 'MainWindow::mousePressEvenet' hides overloaded virtual function. So, that means that I need to create a separate class for my QGraphicsScene that has QGraphicsRectItems?

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

          @privatepepper

          Just do what's suggested there.
          Change your line to:

          void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent) override;
          

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

          ~E. W. Dijkstra

          privatepepperP 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @privatepepper

            Just do what's suggested there.
            Change your line to:

            void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent) override;
            
            privatepepperP Offline
            privatepepperP Offline
            privatepepper
            wrote on last edited by privatepepper
            #5

            @Pl45m4 said in Detecting left mouse click:

            void mousePressEvent(QGraphicsSceneMouseEvent *mouseevent) override;

            Thank you for the reply:), but when I changed my line to yours, I received new error - error: non-virtual member function marked 'override' hides virtual member function

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

              Hi,

              A QMainWindow is not a QGraphicsScene. Take a good look at the method signatures of both these classes.

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

              privatepepperP 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                A QMainWindow is not a QGraphicsScene. Take a good look at the method signatures of both these classes.

                privatepepperP Offline
                privatepepperP Offline
                privatepepper
                wrote on last edited by privatepepper
                #7

                @SGaist

                Thanks for the reply:), I will look into that, I thought that there is no difference where you create a
                mousePressEvent, oh and by the way, I found another solution, I will loop through QGraphicsRectItem and if that rect isUnderMouse I will paint a wall there.
                It's not perfect solution, but at least it works:)

                void MainWindow::keyPressEvent(QKeyEvent *event){
                
                    if (event->key() == Qt::Key_Space){
                
                        for (int y = 0; y < height; y++){
                            for (int x = 0; x < width; x++){
                
                                if (cells[y][x]->isUnderMouse()){
                                    cells[y][x]->setBrush(Qt::red);
                                    vec.cells[y][x] = wall;
                                }
                
                            }
                        }
                    }
                
                }
                
                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