潘&放大所有QWidget儿童

时间:2014-12-29 01:45:08

标签: python pyside qwidget qtgui paintevent

我想有一个可以在其中平移和缩放的QWidget。

情景:

  • 当用户使用鼠标中键向上/向下滚动时,所有子项都会从窗口小部件的中心缩放。
  • 如果用户按住Alt键,并按下鼠标中键,则所有孩子都会使用鼠标的距离进行翻译。

这个想法是通过继承另一个QWidget来创建一个新的QWidget。所以我用QGraphicsView作为例子:

更新:

class MyQGraphicsView(QGraphicsView):
    def __init__(self):
        QGraphicsView.__init__(self)
        self.setFocusPolicy(Qt.WheelFocus)
        self.setRenderHints(QPainter.Antialiasing)
        self.altPressed = False
        self.middlePressed = False
        self.rightPressed = False

    def wheelEvent(self, event):
        self.newScale(event.delta(), 1.15)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Alt:
            self.altPressed = True
            QApplication.setOverrideCursor(Qt.OpenHandCursor)

    def keyReleaseEvent(self, event):
        if event.key() == Qt.Key_Alt:
            self.altPressed = False
            QApplication.setOverrideCursor(Qt.ArrowCursor)

    def mousePressEvent(self, event):
        self._dragPos = event.pos()
        if event.button() == Qt.MidButton:
            self.middlePressed = True
        if event.button() == Qt.RightButton:
            self.rightPressed = True

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.MidButton:
            self.middlePressed = False
        if event.button() == Qt.RightButton:
            self.rightPressed = False

    def mouseMoveEvent(self, event):
        if self.altPressed:
            newPos = event.pos()

            if self.middlePressed:
                diff = newPos - self._dragPos
                self._dragPos = newPos
                QApplication.setOverrideCursor(Qt.ClosedHandCursor)
                self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - diff.x())
                self.verticalScrollBar().setValue(self.verticalScrollBar().value() - diff.y())
                event.accept()
            if self.rightPressed:
                diff = newPos - self._dragPos
                self._dragPos = newPos
                QApplication.setOverrideCursor(Qt.SizeAllCursor)
                self.newScale(diff.x(), 1.01)

    def newScale(self, operator, factor):
        if operator > 0:
            self.scale(factor, factor)
        if operator < 0:
            self.scale(1.0/factor, 1.0/factor)

这个类适用于平移和缩放,但是当我像这样使用它时:

view = MyQGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
view.setSceneRect(0, 0, 1000, 1000)

button = QPushButton('Hello !')
button.clicked.connect(self.sayHello)
scene.addWidget(button)

我无法点击按钮。我该如何解决?

1 个答案:

答案 0 :(得分:1)

最后,我失去了在我的覆盖函数上添加超级构造函数,例如:

super(MyQGraphicsView, self).mouseMoveEvent(event)