调整窗口大小时QRubberBand移动

时间:2019-06-02 07:16:19

标签: python pyqt pyqt5 qlabel qrubberband

我想进行选择,所以我用QRubberband创建了一个QRect,一切正常,当我按下按钮时,我可以单击o进行选择,问题是当我调整窗口大小时,选择正在移动,我该如何阻止这种行为?

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

这是我的主要课程:

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_())

在调整大小之前: enter image description here

调整大小后:

enter image description here

1 个答案:

答案 0 :(得分:3)

问题在于QLabel的对齐方式,默认情况下它对齐到左侧并垂直居中(Qt::AlignLeft | Qt::AlignVCenter),因此图像坐标系的原点与QLabel不一致。一种可能的解决方案是在Qt::AlignLeft | Qt::AlignTop中建立对齐方式,以使坐标系的原点匹配:

self.label = QtWidgets.QLabel()
self.label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)