Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Mark a rectangle when pressing and releasing left mouse button
Forum Updated to NodeBB v4.3 + New Features

Mark a rectangle when pressing and releasing left mouse button

Scheduled Pinned Locked Moved Unsolved Qt for Python
15 Posts 3 Posters 1.6k 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.
  • J john_hobbyist

    @JonB said in Mark a rectangle when pressing and releasing left mouse button:

    @john_hobbyist
    You show code which does some manipulation of a string. How should anybody how that is/might be related in any way to starting and ending coordinates of a mouse button presses? Really.

    If there is any issue in this code start by removing the try ... except. And "never" write except Exception: pass, certainly not while developing, unless you really know what you are doing.

    When I press the mouse left button it should store the starting (x, y) coordinates and when I release the mouse left button it should store the ending (x, y) coordinates.

    So do exactly that, what is there to ask? Store the starting coordinates from the mouse down in some member variable (e.g. QPoint). In the mouse up use the new coordinates plus the same coordinates to draw your rectangle.

    You might also be interested in QRubberBand.

    Ok, I am trying to connect this guy's code: https://stackoverflow.com/questions/67638434/detect-single-mouse-click-in-pyqt5-widgets-missing-mouseclickevent-function with what I have posted above, let's see if it works...

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #4

    @john_hobbyist
    Indeed that uses the principle. mousePressEvent() saves self.pressPos = event.pos(). In mouseReleaseEvent() the new event.pos() gives the mouse-up position, and self.pressPos still has the mouse-down position, so use those for your rectangle.

    JonBJ 1 Reply Last reply
    2
    • J Offline
      J Offline
      john_hobbyist
      wrote on last edited by
      #5

      I put this:

              pressPos = None
              clicked = Signal()
      
              def mousePressEvent(self, event):
                  if event.button() == Qt.LeftButton:
                      self.pressPos = event.pos()
                      print("self.c_x = ", self.c_x)
                      print("self.c_y = ", self.c_y)
      
              def mouseReleaseEvent(self, event):
                  # ensure that the left button was pressed *and* released within the
                  # geometry of the widget; if so, emit the signal;
                  if (self.pressPos is not None and 
                      event.button() == Qt.LeftButton and 
                      event.pos() in self.rect()):
                          self.clicked.emit()
                          print("self.c_x = ", self.c_x)
                          print("self.c_y = ", self.c_y)
                  self.pressPos = None
      

      inside the

      class MyToolBar(mpl_qt.NavigationToolbar2QT)
      

      It does not work!

      self.c_x, and self.c_y
      

      are the coordinates

      jsulmJ JonBJ 2 Replies Last reply
      0
      • J john_hobbyist

        I put this:

                pressPos = None
                clicked = Signal()
        
                def mousePressEvent(self, event):
                    if event.button() == Qt.LeftButton:
                        self.pressPos = event.pos()
                        print("self.c_x = ", self.c_x)
                        print("self.c_y = ", self.c_y)
        
                def mouseReleaseEvent(self, event):
                    # ensure that the left button was pressed *and* released within the
                    # geometry of the widget; if so, emit the signal;
                    if (self.pressPos is not None and 
                        event.button() == Qt.LeftButton and 
                        event.pos() in self.rect()):
                            self.clicked.emit()
                            print("self.c_x = ", self.c_x)
                            print("self.c_y = ", self.c_y)
                    self.pressPos = None
        

        inside the

        class MyToolBar(mpl_qt.NavigationToolbar2QT)
        

        It does not work!

        self.c_x, and self.c_y
        

        are the coordinates

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @john_hobbyist What exactly does not work?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        2
        • J john_hobbyist

          I put this:

                  pressPos = None
                  clicked = Signal()
          
                  def mousePressEvent(self, event):
                      if event.button() == Qt.LeftButton:
                          self.pressPos = event.pos()
                          print("self.c_x = ", self.c_x)
                          print("self.c_y = ", self.c_y)
          
                  def mouseReleaseEvent(self, event):
                      # ensure that the left button was pressed *and* released within the
                      # geometry of the widget; if so, emit the signal;
                      if (self.pressPos is not None and 
                          event.button() == Qt.LeftButton and 
                          event.pos() in self.rect()):
                              self.clicked.emit()
                              print("self.c_x = ", self.c_x)
                              print("self.c_y = ", self.c_y)
                      self.pressPos = None
          

          inside the

          class MyToolBar(mpl_qt.NavigationToolbar2QT)
          

          It does not work!

          self.c_x, and self.c_y
          

          are the coordinates

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #7

          @john_hobbyist
          As @jsulm has asked.....

          Aren't your self.c_... variables just the same in mouseReleaseEvent() as they were in mousePressEvent()? Why should they change. If you think self.clicked.emit() will cause mousePressEvent() to be executed it won't, test that....

          I said to use the event.pos() in mouseReleaseEvent() to get the current coordinates, and use self.c_... from the earlier mousePressEvent() as the previous coordinates. Anything else is more complex/incorrect.

          1 Reply Last reply
          2
          • jsulmJ jsulm

            @john_hobbyist What exactly does not work?

            J Offline
            J Offline
            john_hobbyist
            wrote on last edited by john_hobbyist
            #8

            @jsulm said in Mark a rectangle when pressing and releasing left mouse button:

            @john_hobbyist What exactly does not work?

            It does not print the coordinates.... I am searching it based on the following comment of @JonB

            JonBJ 1 Reply Last reply
            0
            • J john_hobbyist

              @jsulm said in Mark a rectangle when pressing and releasing left mouse button:

              @john_hobbyist What exactly does not work?

              It does not print the coordinates.... I am searching it based on the following comment of @JonB

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #9

              @john_hobbyist said in Mark a rectangle when pressing and releasing left mouse button:

              It does print the coordinates

              What does? Since your print statements are identical in mousePressEvent() and mouseReleaseEvent() it's not ideal for distinguishing which one is being shown!

              And even if the clicked does cause mousePressEvent() to be called, they will then show as identical in mouseReleaseEvent() either way. Since the only place you do self.pressPos = event.pos() is in mousePressEvent().

              J 1 Reply Last reply
              1
              • JonBJ JonB

                @john_hobbyist said in Mark a rectangle when pressing and releasing left mouse button:

                It does print the coordinates

                What does? Since your print statements are identical in mousePressEvent() and mouseReleaseEvent() it's not ideal for distinguishing which one is being shown!

                And even if the clicked does cause mousePressEvent() to be called, they will then show as identical in mouseReleaseEvent() either way. Since the only place you do self.pressPos = event.pos() is in mousePressEvent().

                J Offline
                J Offline
                john_hobbyist
                wrote on last edited by john_hobbyist
                #10

                @JonB said in Mark a rectangle when pressing and releasing left mouse button:

                @john_hobbyist said in Mark a rectangle when pressing and releasing left mouse button:

                It does print the coordinates

                What does? Since your print statements are identical in mousePressEvent() and mouseReleaseEvent() it's not ideal for distinguishing which one is being shown!

                I forgot "not" word! :-) So sorry for that! See my comment again please... I am searching it, though, on how to fix the code based on your comments.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @john_hobbyist
                  Indeed that uses the principle. mousePressEvent() saves self.pressPos = event.pos(). In mouseReleaseEvent() the new event.pos() gives the mouse-up position, and self.pressPos still has the mouse-down position, so use those for your rectangle.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #11

                  @JonB said in Mark a rectangle when pressing and releasing left mouse button:

                  In mouseReleaseEvent() the new event.pos() gives the mouse-up position, and self.pressPos still has the mouse-down position, so use those for your rectangle.

                  J 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @JonB said in Mark a rectangle when pressing and releasing left mouse button:

                    In mouseReleaseEvent() the new event.pos() gives the mouse-up position, and self.pressPos still has the mouse-down position, so use those for your rectangle.

                    J Offline
                    J Offline
                    john_hobbyist
                    wrote on last edited by john_hobbyist
                    #12
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      john_hobbyist
                      wrote on last edited by john_hobbyist
                      #13

                      Although the above scheme works, it disables the zoom function. I will stay to built-in methods. But how can I transform the following method in order to get the coordinates when I release the mouse button?

                      def onPress(self, event):
                              if (event.xdata is None) or (event.ydata is None):
                                  self.mouseClickPos = None
                                  return
                              self.mouseClickPos = int(round(event.xdata)), int(round(event.ydata))
                      

                      (source: https://github.com/Axel-Erfurt/OrthoViewLite/blob/main/OrthoViewLite.py)

                      I found this: https://www.geeksforgeeks.org/pyqt5-qcalendarwidget-setting-mouse-release-event/ but it does not accept event.xdata and event.ydata. I am referring to mouseReleaseEvent method.... Any ideas?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        john_hobbyist
                        wrote on last edited by john_hobbyist
                        #14

                        I also tried this:

                        event.pos().x()
                        event.pos().y()
                        

                        but it gives me wrong coordinates, related to the Canvas and not related to the image.

                        (source: https://books.google.gr/books?id=_g6fDwAAQBAJ&printsec=frontcover#v=onepage&q&f=false)

                        Someone? Thank you!

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          john_hobbyist
                          wrote on last edited by john_hobbyist
                          #15

                          Even this:

                          def mouseReleaseEvent(self, event):
                              print("Mouse button released!")
                              print("coordinates:", event.pos())
                          

                          displays canvas coordinates, which are much lower, and not the image x,y, coordinates....

                          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