PySide6 GUI 编程(41):QTableView 与 QTreeView

2024-09-01 21:50:30 浏览数 (3)

QTableView

示例代码

代码语言:python代码运行次数:0复制
from __future__ import annotations

import sys

from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QApplication, QMainWindow, QTableView, QVBoxLayout, QWidget


class TableModel(QStandardItemModel):
    def __init__(self, rows: int, columns: int) -> None:
        super().__init__(rows, columns)
        self._headers = [f"Header {i   1}" for i in range(columns)]
        self.setHorizontalHeaderLabels(self._headers)

    def add_item(self, row: int, col: int, text: str) -> None:
        item = QStandardItem(text)
        self.setItem(row, col, item)


class TableView(QTableView):
    def __init__(self, model: TableModel) -> None:
        super().__init__()
        self.setModel(model)


class MainWindow(QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        self.setWindowTitle("QTableView MVC Example")

        self.model = TableModel(2, 2)
        self.model.add_item(0, 0, "Item 1-1")
        self.model.add_item(0, 1, "Item 1-2")
        self.model.add_item(1, 0, "Item 2-1")
        self.model.add_item(1, 1, "Item 2-2")

        self.view = TableView(self.model)

        self.container = QWidget()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.view)
        self.container.setLayout(self.layout)
        self.setCentralWidget(self.container)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

运行效果

table view 效果table view 效果

QTreeView

示例代码

代码语言:python代码运行次数:0复制
from __future__ import annotations

import sys

from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView, QVBoxLayout, QWidget


def add_item(parent: QStandardItem, text: str) -> None:
    item = QStandardItem(text)
    parent.appendRow(item)


class TreeModel(QStandardItemModel):
    def __init__(self):
        super().__init__()
        self.setHorizontalHeaderLabels(['Item'])


class TreeView(QTreeView):
    def __init__(self, model: TreeModel) -> None:
        super().__init__()
        self.setModel(model)


class MainWindow(QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        self.setWindowTitle("QTreeView MVC Example")

        self.model = TreeModel()

        root_item = self.model.invisibleRootItem()
        item1 = QStandardItem('Item 1')
        item2 = QStandardItem('Item 2')
        root_item.appendRow(item1)
        root_item.appendRow(item2)

        add_item(item1, 'Item 1-1')
        add_item(item1, 'Item 1-2')
        add_item(item2, 'Item 2-1')
        add_item(item2, 'Item 2-2')

        self.view = TreeView(self.model)

        self.container = QWidget()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.view)
        self.container.setLayout(self.layout)
        self.setCentralWidget(self.container)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

运行效果

tree view 效果tree view 效果

0 人点赞