PySide公司QtGui.QGraphicsWidget子父变换

2024-05-16 09:54:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于使用QtGui.QGraphicsWidget. 在一个给定的例子中,我将试图描述我的问题。其中有两个QGraphicsWidget实例QtGui.qtGraphicscene,其中一个是父级(应用程序_widget.main_小部件-一个是蓝色的,另一个是它的子部件(应用程序_小部件.subwidget-红色的)。如果你运行这个程序,用键盘上的加号键,你就可以在状态间循环变换控件。在

# --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.QGraphicsWidget继承自的属性QtGui.QGraphicsObject),子控件也会被缩放。但是,当我更改父控件的几何图形时(更改“geometry”属性-QtGui.QGraphicsWidget属性),子控件几何体保持不变。在

我运行的是python2.7.6、PySide版本1.2.1和QtCore版本4.8.6。在

为什么子控件不总是跟随父控件的转换?有没有办法只缩放父控件的一个轴并使所有子控件按比例缩放?在


Tags: selfappinitmaindefmachinewidgetchange