如何将小部件放在场景QGraphicsScene上

时间:2018-12-16 23:21:26

标签: python python-3.x qgraphicsview qgraphicsscene pyside2

我想在中间对齐场景。我的问题是,当您打开应用程序时,小部件在中间下方以某种方式对齐,但是将鼠标悬停在按钮或标签上之后,小部件在中间对齐。我可以解决这个问题吗?有什么正确的方法可以使场景在视图中间对齐?

这是我的应用程序的全部代码:

import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setFixedSize(QSize(1080, 720))
        self.setWindowTitle('Graphics Scene')
        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)

        self.mainview = QGraphicsView(self.centralwidget)
        self.mainview.setGeometry(QRect(0, 0, 1080, 720))

        alignment = Qt.Alignment(Qt.AlignVCenter, Qt.AlignHCenter)
        self.mainview.setAlignment(alignment)

        # Main Scene (1)
        self.mainScene = QGraphicsScene()
        self.mainview.setScene(self.mainScene)

        self.labelTitle = QLabel('Main Scene')
        self.labelTitle.setStyleSheet('''
            QLabel {
                font: 48px 'Calibri';
                color: #999;
            }
        ''')

        styleForButtons = ('''
            QPushButton {
                margin: 0 70px;
                padding: 5px 30px;
            }
        ''')

        sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
        self.buttonPlay = QPushButton('Play')
        self.buttonSettings = QPushButton('Settings')
        self.buttonCredits = QPushButton('Credits')
        self.buttonMainExit = QPushButton('Exit')

        self.buttonPlay.setStyleSheet(styleForButtons)
        self.buttonSettings.setStyleSheet(styleForButtons)
        self.buttonCredits.setStyleSheet(styleForButtons)
        self.buttonMainExit.setStyleSheet(styleForButtons)

        self.buttonPlay.setSizePolicy(sizePolicy)
        self.buttonSettings.setSizePolicy(sizePolicy)
        self.buttonCredits.setSizePolicy(sizePolicy)
        self.buttonMainExit.setSizePolicy(sizePolicy)

        self.itemLabelTitle = self.mainScene.addWidget(self.labelTitle)
        self.itemButtonPlay = self.mainScene.addWidget(self.buttonPlay)
        self.itemButtonSettings = self.mainScene.addWidget(self.buttonSettings)
        self.itemButtonCredits = self.mainScene.addWidget(self.buttonCredits)
        self.itemButtonMainExit = self.mainScene.addWidget(self.buttonMainExit)

        self.mainMenuLayout = QGraphicsGridLayout()

        self.mainMenuLayout.addItem(self.itemLabelTitle, 0, 0, alignment)
        self.mainMenuLayout.addItem(self.itemButtonPlay, 1, 0)
        self.mainMenuLayout.addItem(self.itemButtonSettings, 2, 0)
        self.mainMenuLayout.addItem(self.itemButtonCredits, 3, 0)
        self.mainMenuLayout.addItem(self.itemButtonMainExit, 4, 0)

        self.mainMenuWidget = QGraphicsWidget()
        self.mainMenuWidget.setLayout(self.mainMenuLayout)
        self.mainScene.addItem(self.mainMenuWidget)

def main():
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

UPD:上传了我所看到的屏幕截图

enter image description here

我想了解为什么我的应用程序的行为如此奇怪:首先,为什么小部件未在中心精确对齐,其次,为什么会有偏移

1 个答案:

答案 0 :(得分:2)

引起此问题的原因是场景没有设置sceneRect(),因此当显示QGraphicsView时,它会以屏幕分辨率为基础来设置场景的大小,以便最初可以大小超过所有项目的boundingRect(),以便在经过场景之后,相对于该大小而不是viewport()中显示的场景部分居中用鼠标调用update()方法,它会更新为正确的大小。因此,一种解决方案是使用场景坐标中的小部件的大小来设置sceneRect()

self.mainScene.setSceneRect(QRectF(QPointF(), self.mainMenuWidget.sizeHint(Qt.PreferredSize))) 
self.mainScene.addItem(self.mainMenuWidget)