pyqt中QTreeView的简单使用

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from PySide import QtGui
from PySide import QtCore

import sys, os

class TreeView(QtGui.QTreeView):
    def __init__(self, parent=None):
        super(TreeView, self).__init__(parent)

        self.__model = QtGui.QFileSystemModel()
        self.__model.setRootPath(QtCore.QDir.rootPath())
        self.setModel(self.__model)

        self.__current_select_path = 
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PyQt5 QTreeView is a flexible and powerful widget for displaying hierarchical data. It is used to display data in a tree-like structure with parent-child relationships. QTreeView class inherits from QAbstractItemView class, which provides the basic functionality for displaying data from models created by QAbstractItemModel. To use QTreeView in PyQt5, you first need to create a model that inherits from QAbstractItemModel, which is responsible for managing the data to be displayed in the tree view. Here is an example of creating a simple QTreeView: ```python from PyQt5.QtWidgets import QApplication, QTreeView from PyQt5.QtCore import QAbstractItemModel, QModelIndex, Qt class TreeModel(QAbstractItemModel): def __init__(self, data, parent=None): super().__init__(parent) self._data = data def columnCount(self, parent): return 1 def rowCount(self, parent): if not parent.isValid(): return len(self._data) return 0 def data(self, index, role): if not index.isValid(): return None if role == Qt.DisplayRole: return self._data[index.row()] return None def index(self, row, column, parent): if not parent.isValid(): return self.createIndex(row, column) return QModelIndex() def parent(self, index): return QModelIndex() if __name__ == '__main__': app = QApplication([]) data = ['Item 1', 'Item 2', 'Item 3', 'Item 4'] model = TreeModel(data) tree_view = QTreeView() tree_view.setModel(model) tree_view.show() app.exec_() ``` In this example, we have created a TreeModel class that inherits from QAbstractItemModel. The model takes a list of data as an argument and implements the required methods for managing the data, such as rowCount(), columnCount(), data(), index(), and parent(). We then create an instance of the TreeModel class with the data list and set it as the model for the QTreeView widget. Finally, we show the QTreeView widget and start the application event loop. This example creates a very basic tree view with only one column and no parent-child relationships. In practice, you would typically create a more complex model that represents hierarchical data with multiple columns and parent-child relationships. QTreeView provides a lot of customization options, such as customizing the appearance of the tree view, drag and drop support, and editing items in the tree view. You can find more information about these features in the PyQt5 documentation.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值