我是pyqt的新手并试图创建一组嵌套容器来保存我的控件。我找不到任何嵌套小部件的例子(并保持它们的布局)。我只能嵌套布局,但这不是我想要实现的。我想这样做的一个原因是对我的容器的backgroundcolor进行控制。由于布局没有颜色,我想我需要QWidgets或QFrame。这是我走了多远:
class AssetCreationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(AssetCreationWindow, self).__init__()
self.create_content()
self.show()
def create_content(self):
# creating main container-frame, parent it to QWindow
self.main_CF = QtWidgets.QFrame(self)
self.main_CF.setStyleSheet('background-color: rgba(255, 0, 0, 1);')
self.setCentralWidget(self.main_CF)
# creating layout and parent it to main container
# is it correct, that main_CL now manages children of main_CF ?
self.main_CL = QtWidgets.QVBoxLayout(self.main_CF)
# creating the first subcontainer + layout, parenting it
asset_CGF = QtWidgets.QFrame(self.main_CF)
asset_CGF.setStyleSheet('background-color: rgba(0, 255, 0, 1);')
asset_CGL = QtWidgets.QHBoxLayout(asset_CGF)
# creating label and lineEdit, both are supposed to be on top of asset_CGF
asset_label = QtWidgets.QLabel("Assetname: ", asset_CGF)
asset_CGL.addWidget(asset_label)
asset_name = QtWidgets.QLineEdit("MyNewAsset", asset_CGF)
asset_CGL.addWidget(asset_name)
# doing the same with a second container
department_CGF = QtWidgets.QFrame(self.main_CF)
department_CGF.setStyleSheet('background-color: rgba(0, 0, 255, 1);')
department_CGL = QtWidgets.QHBoxLayout(department_CGF)
department_label = QtWidgets.QLabel("Department: ", department_CGF)
department_CGL.addWidget(department_label)
department_names = QtWidgets.QComboBox(department_CGF)
department_CGL.addWidget(department_names)
不幸的是,这种方法将右上角的所有小部件堆叠在一起。另一个是从除main_CL之外的所有布局中删除ParentWidget并使用addLayout()。
def create_content(self):
self.main_CF = QtWidgets.QFrame(self)
self.setCentralWidget(self.main_CF)
self.main_CL = QtWidgets.QVBoxLayout(self.main_CF)
asset_CGF = QtWidgets.QFrame(self.main_CF)
asset_CGF.setStyleSheet('background-color: rgba(255, 0, 0, 1);')
asset_CGL = QtWidgets.QHBoxLayout()
self.main_CL.addLayout(asset_CGL)
asset_label = QtWidgets.QLabel("Asset Name: ", asset_CGF)
asset_CGL.addWidget(asset_label)
asset_name = QtWidgets.QLineEdit("MyNewAsset", asset_CGF)
asset_CGL.addWidget(asset_name)
department_CGF = QtWidgets.QFrame(self.main_CF)
department_CGF.setStyleSheet('background-color: rgba(0, 255, 0, 1);')
department_CGL = QtWidgets.QHBoxLayout()
self.main_CL.addLayout(department_CGL)
department_label = QtWidgets.QLabel("Department: ", department_CGF)
department_CGL.addWidget(department_label)
department_names = QtWidgets.QComboBox(department_CGF)
department_CGL.addWidget(department_names)
这一般看起来更好但是子容器布局似乎不知道子容器。即使控制器是子集装箱的主体,控制器也不在副集装箱的顶端。子容器再次堆叠在右上角。我在我的智慧结束。
答案 0 :(得分:8)
您必须将布局设置为窗口小部件。 E.g
self.main_CL = QtWidgets.QVBoxLayout()
self.main_CF.setLayout(self.main_CL)
然后明确地将子窗口小部件添加到布局中。 E.g。
self.main_CL.addWidget(self.asset_CGF)
对子组件采用相同的模式。
self.asset_CGF.setLayout(self.asset_CGL)
self.asset_CGL.addWidget(...)
实际上你的第一个例子只缺少两个电话
self.main_CL.addWidget(self.asset_CGF)
self.main_CL.addWidget(department_CGF)
答案 1 :(得分:1)
包括AlešErjavecs回答,这是工作代码示例:
class AssetCreationWindow(QtWidgets.QMainWindow):
def __init__(self, controller):
super(AssetCreationWindow, self).__init__()
self.controller = controller
self.create_content()
self.show()
def create_content(self):
# creating main container-frame, parent it to QWindow
self.main_CF = QtWidgets.QFrame(self)
self.main_CF.setStyleSheet('background-color: rgba(150, 0, 0, 1);')
self.setCentralWidget(self.main_CF)
# creating layout and parent it to main container
# is it correct, that main_CL now manages children of main_CF ?
self.main_CL = QtWidgets.QVBoxLayout(self.main_CF)
# creating the first subcontainer + layout, parenting it
asset_CGF = QtWidgets.QFrame(self.main_CF)
self.main_CL.addWidget(asset_CGF)
asset_CGF.setStyleSheet('background-color: rgba(0, 150, 0, 1);')
asset_CGL = QtWidgets.QHBoxLayout(asset_CGF)
# creating label and lineEdit, both are supposed to be on top of asset_CGF
asset_label = QtWidgets.QLabel("Assetname: ", asset_CGF)
asset_CGL.addWidget(asset_label)
asset_name = QtWidgets.QLineEdit("MyNewAsset", asset_CGF)
asset_CGL.addWidget(asset_name)
# doing the same with a second container
department_CGF = QtWidgets.QFrame(self.main_CF)
self.main_CL.addWidget(department_CGF)
department_CGF.setStyleSheet('background-color: rgba(0, 0, 150, 1);')
department_CGL = QtWidgets.QHBoxLayout(department_CGF)
department_label = QtWidgets.QLabel("Department: ", department_CGF)
department_CGL.addWidget(department_label)
department_names = QtWidgets.QComboBox(department_CGF)
department_CGL.addWidget(department_names)