Mark a rectangle when pressing and releasing left mouse button
-
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....