gpt4 book ai didi

python - PYQT - 多层次嵌套小部件和布局

转载 作者:太空狗 更新时间:2023-10-30 00:42:50 48 4
gpt4 key购买 nike

我是 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)

这看起来总体上更好,但子容器布局似乎不知道子容器。即使 Controller 是子容器的父级, Controller 也不在子容器之上。子容器再次堆叠在右上角。我已经无计可施了。

最佳答案

您必须为小部件设置布局。例如

self.main_CL = QtWidgets.QVBoxLayout()
self.main_CF.setLayout(self.main_CL)

然后将子部件显式添加到布局中。例如

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)

关于python - PYQT - 多层次嵌套小部件和布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46351933/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com