0

我有一个关于使用 QtGui.QGraphicsWidget 的问题。在给定的示例中,我将尝试描述我的问题。QtGui.QtGraphicsScene 内部有两个QGraphicsWidget 实例,一个是父级(app_widget.main_widget - 运行时为蓝色),另一个是其子小部件(app_widget.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_())

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

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

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

4

1 回答 1

1

在qt论坛上回答。

“如果你把它放在你在所述父小部件上设置的布局中,它只会跟随它的父小部件。否则,由你来处理子小部件的定位和大小。”

qt论坛帖子链接:

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

于 2015-12-09T09:38:43.297 回答