pyside6中 如何使用table widget 实现如下界面:第一列共5行可多选,第二列共5行,每行为下拉二选一。
时间: 2024-02-29 07:53:27 浏览: 301
你可以按照以下步骤在 PySide6 中使用 Table Widget 实现该界面:
1. 创建一个 Table Widget,并设置行数和列数为 5。
```python
from PySide6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QComboBox
table = QTableWidget()
table.setRowCount(5)
table.setColumnCount(2)
```
2. 设置第一列为多选框,第二列为下拉列表框。
```python
for row in range(5):
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setCheckState(Qt.Unchecked)
table.setItem(row, 0, item)
combo = QComboBox()
combo.addItem("选项1")
combo.addItem("选项2")
table.setCellWidget(row, 1, combo)
```
3. 获取用户选择的值。
```python
for row in range(5):
item = table.item(row, 0)
if item.checkState() == Qt.Checked:
print(f"第{row+1}行被选中,第二列选择了{table.cellWidget(row, 1).currentText()}")
```
完整代码如下:
```python
from PySide6.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QComboBox, QPushButton, QVBoxLayout, QWidget
from PySide6.QtCore import Qt
app = QApplication([])
table = QTableWidget()
table.setRowCount(5)
table.setColumnCount(2)
for row in range(5):
item = QTableWidgetItem()
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setCheckState(Qt.Unchecked)
table.setItem(row, 0, item)
combo = QComboBox()
combo.addItem("选项1")
combo.addItem("选项2")
table.setCellWidget(row, 1, combo)
button = QPushButton("提交")
layout = QVBoxLayout()
layout.addWidget(table)
layout.addWidget(button)
widget = QWidget()
widget.setLayout(layout)
widget.show()
def on_submit():
for row in range(5):
item = table.item(row, 0)
if item.checkState() == Qt.Checked:
print(f"第{row+1}行被选中,第二列选择了{table.cellWidget(row, 1).currentText()}")
button.clicked.connect(on_submit)
app.exec()
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)