PyQt5:如何获取MainWindow布局? [重复]

问题描述 投票:0回答:2

所以当我尝试向MainWindow添加布局时self.mainLayout = QtWidgets.QVBoxLayout(self.MainWindow)我收到此错误:QLayout: Attempting to add QLayout "" to QMainWindow "MainWindow", which already has a layout

如何获取默认布局?有可能吗?

qt pyqt pyqt5 qt5
2个回答
1
投票

QMainWindow布局是非常自定义的,由中央窗口小部件和其他动态部分(例如菜单,状态栏,工具栏,停靠区)组成。替换它没有多大意义,因为您只需从普通的QWidget开始。

您通常要在QMainWindow上编辑的布局是其centralWidget的布局。您可以get / set对其进行相应的操作(包括布局)。


0
投票

正如其他人的回答,QMainWindow有其自己的布局。

但是,如果您想将固定大小的窗口小部件添加到主窗口的中央窗口小部件,可能可以使用要添加到中央窗口小部件的窗口小部件的setFixedSize方法。

示例:

centralWidget = QtGui.QWidget(self)
layout = QtGui.QHBoxLayout(centralWidget)

#Set the fixed size
anotherWidgetOne.setFixedSize(20,20)

#Add other widgets to the central widget
layout.addWidget(self.anotherWidgetOne)

#Set the central widget
self.setCentralWidget(self.centralWidget)
© www.soinside.com 2019 - 2024. All rights reserved.