下面的代码中,tablewidget5的行数不会像tablewidget4一样随checkbox1,checkbox2,checkbox3的勾选数量动态变化,帮我修正:from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QCheckBox, QPushButton, QTableWidgetItem, QTableWidget import sys from PySide6.QtCore import Qt class MyWidget(QWidget): def init(self): super().init() self.initUI() def initUI(self): # 设置窗口大小和标题 self.setGeometry(300, 300, 500, 300) self.setWindowTitle('My Widget') # 创建组件 self.checkbox1 = QCheckBox('Option 1') self.checkbox2 = QCheckBox('Option 2') self.checkbox3 = QCheckBox('Option 3') self.checkbox4 = QCheckBox('Option 4') self.checkbox5 = QCheckBox('Option 5') self.pushbutton = QPushButton('Create Table') self.pushbutton.clicked.connect(self.createTable) # 创建布局 hbox1 = QHBoxLayout() hbox1.addWidget(self.checkbox1) hbox1.addWidget(self.checkbox2) hbox1.addWidget(self.checkbox3) hbox2 = QHBoxLayout() hbox2.addWidget(self.checkbox4) hbox2.addWidget(self.checkbox5) hbox3 = QHBoxLayout() hbox3.addWidget(self.pushbutton) vbox = QVBoxLayout() vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addLayout(hbox3) self.setLayout(vbox) def createTable(self): is_checked_1 = self.checkbox1.isChecked() is_checked_2 = self.checkbox2.isChecked() is_checked_3 = self.checkbox3.isChecked() is_checked_4 = self.checkbox4.isChecked() is_checked_5 = self.checkbox5.isChecked() # 先清空之前的表格 for i in reversed(range(self.layout().count())): widgetToRemove = self.layout().itemAt(i).widget() if widgetToRemove: self.layout().removeWidget(widgetToRemove) widgetToRemove.setParent(None) # 获取勾选项的数量 rows = 0 if is_checked_1: rows += 1 if is_checked_2: rows += 1 if is_checked_3: rows += 1 columns = 2 # 添加表格到布局中 if rows > 0: tableWidget = QTableWidget() tableWidget.setRowCount(rows) tableWidget.setColumnCount(columns) tableWidget.setHorizontalHeaderLabels(['Column 1', 'Column 2']) if is_checked_4: tableWidget.setObjectName('4') self.layout().addWidget(tableWidget) if is_checked_5: tableWidget.setObjectName('5') self.layout().addWidget(tableWidget) # tableWidget = QTableWidget() # tableWidget.setObjectName('5') # tableWidget.setRowCount(1) # tableWidget.setColumnCount(columns) # tableWidget.setHorizontalHeaderLabels(['Column 1', 'Column 2']) # self.layout().addWidget(tableWidget) def closeEvent(self, event): super().closeEvent(event) sys.exit() if name == 'main': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec())
时间: 2024-03-07 21:52:02 浏览: 156
在createTable方法中,你只根据勾选项的数量来确定行数,但没有考虑勾选项的具体内容。你可以使用一个列表来存储所有勾选的选项,然后根据列表的长度来确定行数。以下是修改后的代码:
```
from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QCheckBox, QPushButton, QTableWidgetItem, QTableWidget
import sys
from PySide6.QtCore import Qt
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口大小和标题
self.setGeometry(300, 300, 500, 300)
self.setWindowTitle('My Widget')
# 创建组件
self.checkbox1 = QCheckBox('Option 1')
self.checkbox2 = QCheckBox('Option 2')
self.checkbox3 = QCheckBox('Option 3')
self.checkbox4 = QCheckBox('Option 4')
self.checkbox5 = QCheckBox('Option 5')
self.pushbutton = QPushButton('Create Table')
self.pushbutton.clicked.connect(self.createTable)
# 创建布局
hbox1 = QHBoxLayout()
hbox1.addWidget(self.checkbox1)
hbox1.addWidget(self.checkbox2)
hbox1.addWidget(self.checkbox3)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.checkbox4)
hbox2.addWidget(self.checkbox5)
hbox3 = QHBoxLayout()
hbox3.addWidget(self.pushbutton)
vbox = QVBoxLayout()
vbox.addLayout(hbox1)
vbox.addLayout(hbox2)
vbox.addLayout(hbox3)
self.setLayout(vbox)
def createTable(self):
checked_items = []
if self.checkbox1.isChecked():
checked_items.append(self.checkbox1.text())
if self.checkbox2.isChecked():
checked_items.append(self.checkbox2.text())
if self.checkbox3.isChecked():
checked_items.append(self.checkbox3.text())
if self.checkbox4.isChecked():
checked_items.append(self.checkbox4.text())
if self.checkbox5.isChecked():
checked_items.append(self.checkbox5.text())
rows = len(checked_items)
columns = 2
# 先清空之前的表格
for i in reversed(range(self.layout().count())):
widgetToRemove = self.layout().itemAt(i).widget()
if widgetToRemove:
self.layout().removeWidget(widgetToRemove)
widgetToRemove.setParent(None)
# 添加表格到布局中
if rows > 0:
for item in checked_items:
tableWidget = QTableWidget()
tableWidget.setRowCount(1)
tableWidget.setColumnCount(columns)
tableWidget.setHorizontalHeaderLabels(['Column 1', 'Column 2'])
tableWidget.setObjectName(item)
self.layout().addWidget(tableWidget)
def closeEvent(self, event):
super().closeEvent(event)
sys.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec())
```
在修改后的代码中,首先定义了一个列表checked_items,用来存储所有勾选的选项。然后根据勾选状态将选项添加到列表中,最后根据列表长度来确定行数。在添加表格到布局中时,对于每个选项都创建一个新的表格,并设置其对象名称为选项的文本。这样,每个表格就可以独立更新了。
阅读全文