pyside6 Qtreeview中下拉选项怎么添加复选框
时间: 2024-04-22 19:04:35 浏览: 230
要在PySide6的QTreeView中添加复选框,需要使用QStandardItemModel和QStandardItem类来创建模型和项。然后,将复选框添加到项中并将项添加到模型中。
以下是一个示例代码,展示如何在QTreeView中添加复选框:
```python
from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItemModel, QStandardItem
from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Create the model and set column headers
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['Name', 'Checked'])
# Create some items with checkboxes
item1 = QStandardItem('Item 1')
item1.setCheckable(True)
item2 = QStandardItem('Item 2')
item2.setCheckable(True)
# Add the items to the model
model.appendRow([item1, QStandardItem()])
model.appendRow([item2, QStandardItem()])
# Create the tree view and set the model
tree_view = QTreeView()
tree_view.setModel(model)
# Set the checkbox column to be editable
tree_view.setItemDelegateForColumn(1, tree_view.itemDelegateForColumn(0))
# Add the tree view to the main window
self.setCentralWidget(tree_view)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec()
```
在这个例子中,我们创建了一个QStandardItemModel,然后创建两个带有复选框的QStandardItem。我们将这些项添加到模型中,并将模型设置为QTreeView的模型。最后,我们使用setItemDelegateForColumn方法将复选框列设置为可编辑的,并将QTreeView添加到主窗口中。
阅读全文
相关推荐
![](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)
![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://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)