【发布时间】:2018-03-03 06:41:06
【问题描述】:
我是 pyqt 的新手,并试图创建一组嵌套容器来保存我的控件。我找不到任何嵌套小部件的示例(并保持它们的布局)。我只能嵌套布局,但这不是我想要实现的。我想这样做的一个原因是控制我的容器的背景颜色。由于布局没有颜色,我想我需要 QWidgets 或 QFrames。这是我走了多远:
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)
这看起来总体上更好,但子容器布局似乎不知道子容器。即使控制器是子容器的父级,但控制器并不位于子容器之上。子容器再次堆叠在右上角。我无计可施。
【问题讨论】:
-
你可以展示你想要的图片。