PyQt5 Treeview

PyQt5 支援樹檢視控制元件(類 QTreeView)。在本文中,我們將展示如何使用視窗小控制元件。

![PyQt5 TreeView](/img/Tutorial/PyQt5/PyQt5 TreeView.png)

該影象顯示了一個 QTreeView 小控制元件,其中包含資料。

PyQt5 Treeview 示例

下面的程式碼將使用 QTreeView 類(使用 Python 3 執行)建立樹檢視。資料按程式碼新增到樹檢視中。程式碼的說明在程式碼下面。

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

import sys
from PyQt5.QtGui import QIcon

from PyQt5.QtCore import (QDate, QDateTime, QRegExp, QSortFilterProxyModel, Qt,
        QTime)
from PyQt5.QtGui import QStandardItemModel
from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox, QGridLayout,
        QGroupBox, QHBoxLayout, QLabel, QLineEdit, QTreeView, QVBoxLayout,
        QWidget)


class App(QWidget):

    FROM, SUBJECT, DATE = range(3)

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Treeview Example - tastones.com'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 240
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.dataGroupBox = QGroupBox("Inbox")
        self.dataView = QTreeView()
        self.dataView.setRootIsDecorated(False)
        self.dataView.setAlternatingRowColors(True)

        dataLayout = QHBoxLayout()
        dataLayout.addWidget(self.dataView)
        self.dataGroupBox.setLayout(dataLayout)

        model = self.createMailModel(self)
        self.dataView.setModel(model)
        self.addMail(model, '[email protected]', 'Your Github Donation','03/25/2017 02:05 PM')
        self.addMail(model, '[email protected]', 'Github Projects','02/02/2017 03:05 PM')
        self.addMail(model, '[email protected]', 'Your Phone Bill','01/01/2017 04:05 PM')

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.dataGroupBox)
        self.setLayout(mainLayout)

        self.show()

    def createMailModel(self,parent):
        model = QStandardItemModel(0, 3, parent)
        model.setHeaderData(self.FROM, Qt.Horizontal, "From")
        model.setHeaderData(self.SUBJECT, Qt.Horizontal, "Subject")
        model.setHeaderData(self.DATE, Qt.Horizontal, "Date")
        return model

    def addMail(self,model, mailFrom, subject, date):
        model.insertRow(0)
        model.setData(model.index(0, self.FROM), mailFrom)
        model.setData(model.index(0, self.SUBJECT), subject)
        model.setData(model.index(0, self.DATE), date)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Treeview 解釋

我們使用以下行建立一個新的樹檢視物件:

self.dataView = QTreeView()

檢視設定為具有模型,

self.dataView.setModel(model)

其中模型如下,

model = QStandardItemModel(0, 3, parent)
model.setHeaderData(self.FROM, Qt.Horizontal, "From")
model.setHeaderData(self.SUBJECT, Qt.Horizontal, "Subject")
model.setHeaderData(self.DATE, Qt.Horizontal, "Date")

然後我們使用以下方法新增

model.insertRow(0)
model.setData(model.index(0, self.FROM), mailFrom)
model.setData(model.index(0, self.SUBJECT), subject)
model.setData(model.index(0, self.DATE), date)