python - 选择其中一个单选按钮时动态添加和删除布局
问题描述
我可以在 pyqt 中向 QHBoxLayout 动态添加布局,但是一旦添加就无法删除它们。
主要目的是根据所选的单选按钮动态添加和删除布局。
def SearchTab(self):
self.layout = QVBoxLayout()
button_layout = QHBoxLayout()
radio_button_1 = QRadioButton("Search")
radio_button_2 = QRadioButton("Update")
button_layout.addWidget(radio_button_1)
button_layout.addWidget(radio_button_2)
self.layout.addItem(button_layout)
radio_button_1.toggled.connect(lambda :self.SelectButtonCheck(radio_button_1))
radio_button_1.toggled.connect(lambda :self.UpdateButtonCheck(radio_button_2))
self.setTabText(0,"Search")
self.tab1.setLayout(self.layout)
def SelectButtonCheck(self,b):
if b.text() == "Search":
if b.isChecked():
print(b.text()+ "is selected")
self.pg_details = pgd.PGDetails()
layout = self.pg_details.returnLayout()
self.layout.addLayout(layout)
def UpdateButtonCheck(self,b):
if b.text() == "Update":
if b.isChecked():
print(b.text()+ " is selected")
for i in range(self.layout.count()):
print(self.layout.itemAt(i))
temp_layout = self.layout.itemAt(i)
widget = temp_layout.widget()
temp_layout.removeItem(temp_layout)
if widget is not None:
widget.deleteLater()
初始屏幕-
目前我可以在选择“搜索”单选按钮时添加布局——
但是当我选择“更新”单选按钮时没有任何反应
还可以找到已添加的布局 -
for i in range(self.layout.count()):
print(self.layout.itemAt(i))
<PyQt5.QtWidgets.QHBoxLayout object at 0x1180ec438>
<PyQt5.QtWidgets.QFormLayout object at 0x1180ff828>
正在添加布局,但未删除。
任何线索都会有所帮助,我在这里缺少什么
解决方案
我能够解决这个问题。花了我一段时间,但了解了布局中小部件的排列方式。
我假设删除布局将级联删除小部件本身。
我使用以下函数删除了布局及其对应的小部件。
def SearchTab(self):
self.layout = QVBoxLayout()
button_layout = QHBoxLayout()
radio_button_1 = QRadioButton("Search")
radio_button_2 = QRadioButton("Update")
button_layout.addWidget(radio_button_1)
button_layout.addWidget(radio_button_2)
self.layout.addItem(button_layout)
#createDB()
radio_button_1.toggled.connect(lambda :self.SelectButtonCheck(radio_button_1,self.layout))
radio_button_1.toggled.connect(lambda :self.UpdateButtonCheck(radio_button_2,self.layout))
#layout.addRow("Address",QLineEdit())
self.setTabText(0,"Search")
update_layout = QHBoxLayout()
#update_layout.set
#update_btn = QDialogButtonBox(QDialogButtonBox)
#update_btn.setAlignment(Qt.AlignBottom)
update_layout.setAlignment(QtCore.Qt.AlignTop)
update_btn = QPushButton('Update')
reset_btn = QPushButton('Reset')
#self.layout.addRow(update_layout)
update_layout.addWidget(update_btn)
update_layout.addWidget(reset_btn)
update_btn.clicked.connect(self.createDB)
self.tab1.setLayout(self.layout)
def SelectButtonCheck(self,b,stacklayout):
if b.text() == "Search":
if b.isChecked():
print(b.text()+ "is selected")
self.pg_details = pgd.PGDetails()
layout = self.pg_details.returnLayout()
self.layout.addLayout(layout)
def removeLayout(self,layout):
for i in range(layout.count()):
temp_layout = layout.itemAt(i)
if temp_layout is not None:
widget = temp_layout.widget()
if widget is not None:
widget.deleteLater()
else:
return
if temp_layout.layout() is not None:
self.removeLayout(temp_layout.layout())
def removeFormLayout(self,layout):
if layout is not None:
for i in range(layout.count()):
temp_layout = layout.itemAt(i)
if isinstance(temp_layout.layout(),type(QFormLayout())):
self.removeLayout(temp_layout.layout())
else:
next
else:
return
def UpdateButtonCheck(self,b,stacklayout):
if b.text() == "Update":
if b.isChecked():
print(b.text()+ " is selected")
self.removeFormLayout(stacklayout)
removeFormLayout 函数会挑选出我使用 Search 单选按钮添加的表单布局,并且 removeLayout 也会删除它下面的所有小部件。
对所用方法的建议和改进持开放态度,我尝试了除 FormLayout 之外的几个布局删除。目前它工作正常。
还有人知道如何在顶部对齐 HBoxLayout,单选按钮再次从中间开始,我想将它们对齐在屏幕顶部
推荐阅读
- php - 使用 composer Windows 10 安装 laravel 时出错
- python - 如何将多个值关联到一个字典键?
- java - Compiler says variable has not been initialized
- influxdb - “高基数”字段(或标签)上的 InfluxDB WHERE 子句
- scala - spark scala 将嵌套数据框转换为嵌套数据集
- javascript - 在反应应用程序中安装 Node fs 模块或以其他方式创建目录列表?
- emotion - 何时使用样式化组件与何时在情感中使用 css 道具?
- php - 如何获取我刚刚添加的 id?
- python - 如果列的len == 1,如何在周列数据框熊猫的前面添加0
- python - 在 django 中生成令牌