Mark a rectangle when pressing and releasing left mouse button
-
I use this code: https://github.com/Axel-Erfurt/OrthoViewLite/blob/main/OrthoViewLite.py
Inside this code:
class MyToolBar(mpl_qt.NavigationToolbar2QT): def set_message(self, s): try: sstr = s.split() while len(sstr) > 5: del sstr[0] x, y = float(sstr[0][2:]), float(sstr[1][2:]) s = f'x = {x:.2f}\ny = {y:.2f}' except Exception: pass if self.coordinates: self.locLabel.setText(s)
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. Any ideas how to approach the solution to that?
-
I use this code: https://github.com/Axel-Erfurt/OrthoViewLite/blob/main/OrthoViewLite.py
Inside this code:
class MyToolBar(mpl_qt.NavigationToolbar2QT): def set_message(self, s): try: sstr = s.split() while len(sstr) > 5: del sstr[0] x, y = float(sstr[0][2:]), float(sstr[1][2:]) s = f'x = {x:.2f}\ny = {y:.2f}' except Exception: pass if self.coordinates: self.locLabel.setText(s)
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. Any ideas how to approach the solution to that?
@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" writeexcept 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.
-
@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" writeexcept 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.
@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" writeexcept 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...
-
@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" writeexcept 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...
@john_hobbyist
Indeed that uses the principle.mousePressEvent()
savesself.pressPos = event.pos()
. InmouseReleaseEvent()
the newevent.pos()
gives the mouse-up position, andself.pressPos
still has the mouse-down position, so use those for your rectangle. -
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
-
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
@john_hobbyist What exactly does not work?
-
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
@john_hobbyist
As @jsulm has asked.....Aren't your
self.c_...
variables just the same inmouseReleaseEvent()
as they were inmousePressEvent()
? Why should they change. If you thinkself.clicked.emit()
will causemousePressEvent()
to be executed it won't, test that....I said to use the
event.pos()
inmouseReleaseEvent()
to get the current coordinates, and useself.c_...
from the earliermousePressEvent()
as the previous coordinates. Anything else is more complex/incorrect. -
@john_hobbyist What exactly does not work?
@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
-
@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
@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()
andmouseReleaseEvent()
it's not ideal for distinguishing which one is being shown!And even if the
clicked
does causemousePressEvent()
to be called, they will then show as identical inmouseReleaseEvent()
either way. Since the only place you doself.pressPos = event.pos()
is inmousePressEvent()
. -
@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()
andmouseReleaseEvent()
it's not ideal for distinguishing which one is being shown!And even if the
clicked
does causemousePressEvent()
to be called, they will then show as identical inmouseReleaseEvent()
either way. Since the only place you doself.pressPos = event.pos()
is inmousePressEvent()
.@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()
andmouseReleaseEvent()
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.
-
@john_hobbyist
Indeed that uses the principle.mousePressEvent()
savesself.pressPos = event.pos()
. InmouseReleaseEvent()
the newevent.pos()
gives the mouse-up position, andself.pressPos
still has the mouse-down position, so use those for your rectangle. -
@JonB said in Mark a rectangle when pressing and releasing left mouse button:
In
mouseReleaseEvent()
the newevent.pos()
gives the mouse-up position, andself.pressPos
still has the mouse-down position, so use those for your rectangle.This post is deleted! -
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?
-
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!
-
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....