Merge two pieces of code
-
@john_hobbyist
Hi
It might as then we are sure its the current code that you have right now.
Also others might spot what the issue can be.I think its might be Local / Global coordinate issues but I cannot understand how
the Rubberband can react to MouseEvents from the Canvas widget since its
clearly outside. -
This is the code where I figure out the problem (source: https://stackoverflow.com/questions/34220275/how-to-select-a-region-with-qrubberband-on-a-qlabel-like-in-ksnapshot)
def mousePressEvent(self, event): print("56") ''' Mouse is pressed. If selection is visible either set dragging mode (if close to border) or hide selection. If selection is not visible make it visible and start at this point. ''' if event.button() == QtCore.Qt.LeftButton: print("57") position = QtCore.QPoint(event.pos()) print("58") if self.selection.isVisible(): print("59") # visible selection if (self.upper_left - position).manhattanLength() < 20: print("60") # close to upper left corner, drag it self.mode = "drag_upper_left" print("61") elif (self.lower_right - position).manhattanLength() < 20: print("62") # close to lower right corner, drag it self.mode = "drag_lower_right" print("63") else: print("64") # clicked somewhere else, hide selection self.selection.hide() print("65") else: # no visible selection, start new selection print("66") self.upper_left = position print("67") self.lower_right = position print("68") self.mode = "drag_lower_right" print("69") self.selection.show() print("70") def mouseMoveEvent(self, event): print("71") ''' Mouse moved. If selection is visible, drag it according to drag mode. ''' if self.selection.isVisible(): print("72") # visible selection if self.mode == "drag_lower_right": print("73") self.lower_right = QtCore.QPoint(event.pos()) #self.lower_right = QtCore.QPoint(0, 0) print("74") elif self.mode == "drag_upper_left": print("75") self.upper_left = QtCore.QPoint(event.pos()) #self.upper_left = QtCore.QPoint(10, 10) print("76") # update geometry self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized()) # self.selection.setGeometry(QtCore.QRect(100, 100).normalized()) print("76A") print( QtCore.QRect(self.upper_left, self.lower_right).normalized() ) print("77")
So the things have like this:
I run the code, the GUI starts and I move the mouse inside the image canvas, I continuously get the number:71
When I press the mouse button (and do nothing else, not move the mouse), I get:
56 57 58 66 67 68 69 70
After I pressed the mouse button and I move the mouse, I continuously get:
PyQt5.QtCore.QRect(93, 84, 841, 149) <--- the numbers inside QRect change 77 71 72 73 74 76A
Do you see something strange here?
-
What exactly is your issue ?
If you find the rectangle strange why not print the points used to create it ?
-
@SGaist I am not talking about the printing the values of the rectangle here, but the fact that when I try to use qrubberband...Please look my previous messages, and the images that I have posted in order to help you help me... To sum up the qrubberband rectangle is not shown/visible inside the canvas but is limited on the upper left corner...Thank you for your time..
-
Where is this code implemented exactly ?
-
This post is deleted!
-
@john_hobbyist said in Merge two pieces of code:
Do you see the problem?
Bluntly speaking, your MyMplCanvas
__init__
implementation is a mess. You are calling the init method of QLabel, do some stuff, then call the base class init implementation through super, set the parent independently, then you manage to use a method called imshow which is also a method from OpenCV's highgui module.You really should start by cleaning this up.
Do things step by step.
First question is: taking the rubber band part out of the equation for now, why do you need all these modifications on the MplCanvas class ? Are you sure you are using it correctly ?
-
This post is deleted!