PySide QtGui.QGraphicsWidget子父变换

时间:2015-10-20 15:06:17

标签: python qt pyqt pyside qgraphicswidget

我对使用QtGui.QGraphicsWidget有疑问。在给定的例子中,我将尝试描述我的问题。 QtGui.QtGraphicsScene中有两个QGraphicsWidget实例,其中一个是父(app_widget.main_widget - 运行时为蓝色),另一个是其子窗口小部件(app_widget.subwidget - 运行时为红色窗口小部件)。如果您运行程序,使用键盘上的Plus键,您可以循环转换窗口中的小部件。

# --coding: utf-8 --
# window.py

import sys
from PySide import QtGui, QtCore

class WidgetBase(QtGui.QGraphicsWidget):
    def __init__(self, parent = None):
        super(WidgetBase, self).__init__(parent)

    def set_background(self, color_hex):
        palette = QtGui.QPalette()
        color = QtGui.QColor()
        color.setRgba(color_hex)
        palette.setColor(QtGui.QPalette.Background, color)
        self.setPalette(palette)
        self.setAutoFillBackground(True)


class AppWidget(WidgetBase):
    def __init__(self):
        super(AppWidget, self).__init__()
        self.main_widget = WidgetBase(self)
        self.subwidget = WidgetBase(self.main_widget)


class Window(QtGui.QMainWindow):
    change_app_state = QtCore.Signal()

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        SCREEN_DIM = QtGui.QDesktopWidget().screenGeometry()

        self.app_scene = QtGui.QGraphicsScene(0, 0, SCREEN_DIM.width(), SCREEN_DIM.height())

        app_view = QtGui.QGraphicsView(self.app_scene)
        app_view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        app_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        self.setCentralWidget(app_view)

        app_widget = AppWidget()
        app_widget.main_widget.set_background(0x50449558)
        app_widget.main_widget.resize(500, 500)
        app_widget.subwidget.set_background(0x50ff3300)
        app_widget.subwidget.resize(500 * 0.5, 500)

        self.app_scene.addItem(app_widget)

        self.machine = QtCore.QStateMachine()
        state1 = QtCore.QState(self.machine)
        state2 = QtCore.QState(self.machine)
        state3 = QtCore.QState(self.machine)
        state4 = QtCore.QState(self.machine)

        state1.assignProperty(app_widget.main_widget, 'geometry', QtCore.QRectF(0, 0, 500, 500))
        state2.assignProperty(app_widget.main_widget, 'scale', 0.5)
        state3.assignProperty(app_widget.main_widget, 'scale', 1)
        state4.assignProperty(app_widget.main_widget, 'geometry', QtCore.QRectF(0, 0, 1000, 500))

        trans1 = state1.addTransition(self.change_app_state, state2)
        trans2 = state2.addTransition(self.change_app_state, state3)
        trans3 = state3.addTransition(self.change_app_state, state4)
        trans4 = state4.addTransition(self.change_app_state, state1)

        trans1.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'scale', state1))
        trans2.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'scale', state2))
        trans3.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'geometry', state3))
        trans4.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'geometry', state4))

        self.machine.setInitialState(state1)
        self.machine.start()

    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_Plus:
            self.change_app_state.emit()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.showFullScreen()
    sys.exit(app.exec_())

当缩放父窗口小部件(更改'scale'属性 - 从QtGui.QGraphicsObject继承的QtGui.QGraphicsWidget属性)时,子窗口小部件也会缩放。但是,当我更改父窗口小部件的几何(更改'几何'属性 - QtGui.QGraphicsWidget属性)时,子窗口小部件几何保持不变。

我正在运行Python 2.7.6,PySide版本1.2.1和QtCore版本4.8.6。

为什么子窗口小部件始终不跟随父窗体转换?有没有办法只缩放父窗口小部件的一个轴并让所有子窗口小部件按比例缩放?

1 个答案:

答案 0 :(得分:1)

在qt论坛上回答。

“如果你将它放在你在上述父窗口小部件上设置的布局中,它只会跟随它的父窗口小部件。否则,你可以自己处理子窗口小部件的位置和大小。”

qt论坛帖子链接:

https://forum.qt.io/topic/59958/pyside-qtgui-qgraphicswidget-child-parent-transformations