Solved Matching 2 .tiff images pixel-by-pixel
-
QRubberBand comes to mind.
-
This post is deleted! -
I use this code: https://stackoverflow.com/questions/13840289/how-to-use-qrubberband-with-qrect-class-in-pyqt
inside my widget, more specifically, this code:def mousePressEvent(self, event): self.origin = event.pos() self.rubberband.setGeometry( QtCore.QRect(self.origin, QtCore.QSize())) self.rubberband.show() QtGui.QWidget.mousePressEvent(self, event) def mouseMoveEvent(self, event): if self.rubberband.isVisible(): self.rubberband.setGeometry( QtCore.QRect(self.origin, event.pos()).normalized()) QtGui.QWidget.mouseMoveEvent(self, event) def mouseReleaseEvent(self, event): if self.rubberband.isVisible(): self.rubberband.hide() selected = [] rect = self.rubberband.geometry() for child in self.findChildren(QtGui.QPushButton): if rect.intersects(child.geometry()): selected.append(child) print 'Selection Contains:\n ', if selected: print ' '.join( 'Button: %s\n' % child.text() for child in selected) else: print ' Nothing\n' QtGui.QWidget.mouseReleaseEvent(self, event)
but I don't see any rectangle...
-
Are you sure your methods are called ?
-
I use these method calls:
self.mousePressEvent() self.mouseMoveEvent() self.mouseReleaseEvent()
inside the main widget class..but what do I put inside the parenthesis? Because "event" does not seem to work...
-
@john_hobbyist
What do you mean by "I use these method calls"? These methods are supposed to be overrides. Qt infrastructure calls them when it detects the mouse event, with the appropriateevent
parameter.And anyway @SGaist was asking you put a debugging
print()
statement inside these override methods to make sure they are getting called. -
I am trying some ready examples in order to understand the rationale. I run the first code from here: https://stackoverflow.com/questions/34220275/how-to-select-a-region-with-qrubberband-on-a-qlabel-like-in-ksnapshot
I have changed PyQt4 ---> PyQt5 and QtGui ----> QtWidgets. I also changed this line:
pixmap = QPixmap.grabWindow(app.desktop().winId())
with this:
pixmap = QScreen.grabWindow(app.desktop().winId())
but I get this error:
File "QRubberBandonaQLabelKSnapshot.py", line 47, in initUI pixmap = QScreen.grabWindow(app.desktop().winId()) TypeError: grabWindow(self, sip.voidptr, x: int = 0, y: int = 0, width: int = -1, height: int = -1): first argument of unbound method must have type 'QScreen'
Any idea what to do here?
-
You're using it as if it were a static method which it's not. You need an instance of QScreen.
You can get one from your QApplication.
-
@SGaist Sorry I do not understand...how I get an instance from QApplication in QScreen?
-
It's the other way around.
In any case, you should rather get the screen from your widget.
Take a look at the screenshot example.
-
I just try to see how to run the QRubberBand in PyQt5....
-
Did you check the example from the class documentation ?
-
I added them to the main class. I am trying to call the methods, like this:
self.mousePressEvent(event) self.mouseMoveEvent(event)
etc.
But what do I put inside/instead of "event"?
-
The code you showed higher up to drive the rubber band.
Or the docs @SGaist points to. they explain in detail what is needed.def mousePressEvent(self, event): ///// this set the start location of the band self.origin = event.pos() self.rubberband.setGeometry( QtCore.QRect(self.origin, QtCore.QSize())) self.rubberband.show() QtGui.QWidget.mousePressEvent(self, event) def mouseMoveEvent(self, event): // this makes the band follow the mouse to adjust the size if self.rubberband.isVisible(): self.rubberband.setGeometry( QtCore.QRect(self.origin, event.pos()).normalized()) QtGui.QWidget.mouseMoveEvent(self, event) def mouseReleaseEvent(self, event): // this is the end of the operation and you use the rectange defined by the band if self.rubberband.isVisible(): self.rubberband.hide() rect = self.rubberband.geometry() ... then use rect for whatever you need
-
@mrjj: Thanks for your help! I am searching all morning. Here https://doc.qt.io/qtforpython/PySide6/QtWidgets/QRubberBand.html#more I don't see something about what to put inside the "event". The only I can understand is to use this inside events: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QMouseEvent.html
-
Im not sure what you refer too ?
def mouseReleaseEvent(self, event): <<< this event ? the paramter ?
To use the rubberband, we use 3 functions as shown.
-
@mrjj
Yes, I try this:self.mousePressEvent(event)
and the code requires to put something instead of "event". This is the error:
File "code.py", line 310, in __init__ self.mousePressEvent(event) NameError: name 'event' is not defined
-
Where are you calling these methods from ?
-
@SGaist : Inside this method:
def __init__(self, parent=None): . . . (code) . . . self.mousePressEvent(event) self.mouseMoveEvent(event) self.mouseReleaseEvent(event) . . . (other methods + code per method) . . . def mousePressEvent(self, event): (code) def mouseMoveEvent(self, event): (code) def mouseReleaseEvent(self,event): (code)
which the first method of the class. Any idea?
-
That's not how it works. These methods are automatically called when appropriate.