Qtreeview可以使用选择模型,点一下,选中上下九个
时间: 2024-02-17 21:03:59 浏览: 38
QTreeView 是可以使用选择模式的,可以通过设置 QTreeView 的 selectionMode 属性实现。而选择上下九个的功能,可以通过自定义 QItemSelectionModel 来实现。具体实现方法如下:
1. 继承 QItemSelectionModel 类,重写 select() 方法,在方法中实现选中上下九个的功能。
```python
class MySelectionModel(QtGui.QItemSelectionModel):
def select(self, selection, flags):
if flags & QtGui.QItemSelectionModel.Clear:
super(MySelectionModel, self).select(selection, flags)
else:
newSelection = QtGui.QItemSelection()
for index in selection.indexes():
for i in range(-4, 5):
if i != 0:
newIndex = self.model().index(index.row() + i, index.column())
if newIndex.isValid():
newSelection.select(newIndex, QtGui.QItemSelectionModel.Select)
super(MySelectionModel, self).select(newSelection, flags)
```
2. 在 QTreeView 中设置自定义选择模型。
```python
myModel = QtGui.QStandardItemModel()
myTreeView = QtGui.QTreeView()
mySelectionModel = MySelectionModel(myModel)
myTreeView.setSelectionModel(mySelectionModel)
```
这样,当用户点击 QTreeView 中的某个项时,就会选中该项上下九个。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)