如何在场景 QGraphicsScene 上居中小部件
How to center widgets on scene QGraphicsScene
我想把场景居中对齐。我的问题是,当您打开应用程序时,小部件以某种方式在中间下方对齐,但将鼠标悬停在按钮或标签上后,小部件在中间对齐。我能以某种方式解决这个问题吗?有什么正确的方法可以将场景对齐到视图中间?
这是我的应用程序的全部代码:
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:上传了我看到的截图
我想了解为什么我的应用程序表现如此奇怪:首先,为什么小部件没有完全居中对齐,其次,为什么会有偏移
这个问题是因为场景没有设置sceneRect()
所以当显示QGraphicsView
时它以屏幕的分辨率为基础设置场景的大小所以它最初可以具有所有项目的 boundingRect()
的大小,以便相对于该大小而不是相对于 viewport()
中显示的场景部分居中,就在传递之后使用鼠标的场景调用 update()
方法并更新为正确的大小。因此,一种解决方案是使用场景坐标中小部件的大小来设置 sceneRect()
。
self.mainScene.setSceneRect(QRectF(QPointF(), self.mainMenuWidget.sizeHint(Qt.PreferredSize)))
self.mainScene.addItem(self.mainMenuWidget)
我想把场景居中对齐。我的问题是,当您打开应用程序时,小部件以某种方式在中间下方对齐,但将鼠标悬停在按钮或标签上后,小部件在中间对齐。我能以某种方式解决这个问题吗?有什么正确的方法可以将场景对齐到视图中间?
这是我的应用程序的全部代码:
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:上传了我看到的截图
我想了解为什么我的应用程序表现如此奇怪:首先,为什么小部件没有完全居中对齐,其次,为什么会有偏移
这个问题是因为场景没有设置sceneRect()
所以当显示QGraphicsView
时它以屏幕的分辨率为基础设置场景的大小所以它最初可以具有所有项目的 boundingRect()
的大小,以便相对于该大小而不是相对于 viewport()
中显示的场景部分居中,就在传递之后使用鼠标的场景调用 update()
方法并更新为正确的大小。因此,一种解决方案是使用场景坐标中小部件的大小来设置 sceneRect()
。
self.mainScene.setSceneRect(QRectF(QPointF(), self.mainMenuWidget.sizeHint(Qt.PreferredSize)))
self.mainScene.addItem(self.mainMenuWidget)