I want to make a selection, so I create a QRect with QRubberband, everything work fine, when i press the button I cand click o image and make a selection, the problem is the selection is moving when I resize the window, how can I stop this behviour?
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# from PyQt4 import QtCore, QtWidgets
class ResizableRubberBand(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ResizableRubberBand, self).__init__(parent)
self.draggable = False
self.mousePressPos = None
self.mouseMovePos = None
self._band = QtWidgets.QRubberBand(QtWidgets.QRubberBand.Rectangle, self)
self._band.setGeometry(550, 550, 550, 550)
self._band.show()
self.show()
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.RightButton:
self.mousePressPos = event.globalPos() # global
self.mouseMovePos = event.globalPos() - self.pos() # local
self.draggable = True
elif event.button() == QtCore.Qt.LeftButton:
self.position = QtCore.QPoint(event.pos())
self.upper_left = self.position
self.lower_right = self.position
self.mode = "drag_lower_right"
self._band.show()
def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & QtCore.Qt.RightButton:
globalPos = event.globalPos()
print(globalPos)
diff = globalPos - self.mouseMovePos
self.move(diff)
self.mouseMovePos = globalPos - self.pos()
elif self._band.isVisible():
# visible selection
if self.mode is "drag_lower_right":
self.lower_right = QtCore.QPoint(event.pos())
# print(str(self.lower_right))
elif self.mode is "drag_upper_left":
self.upper_left = QtCore.QPoint(event.pos())
# print(str(self.upper_left))
# update geometry
self._band.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized())
def mouseReleaseEvent(self, event):
self.draggable = False
this is my main class:
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtWidgets.QPushButton('Show Rubber Band')
self.button.clicked.connect(self.handleButton)
self.label = QtWidgets.QLabel()
# self.label.setScaledContents(True)
self.label.setPixmap(QtGui.QPixmap('image.png'))
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.button)
def handleButton(self):
self.band = ResizableRubberBand(self.label)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())
after resize:
The problem is the alignment of the QLabel, by default it is aligned to the left and vertically centered (Qt::AlignLeft | Qt::AlignVCenter
) so that the origin of the coordinate system of the image and the QLabel do not coincide. A possible solution is to establish the alignment in Qt::AlignLeft | Qt::AlignTop
so that the origin of coordinate systems matches:
self.label = QtWidgets.QLabel()
self.label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)