PySide2学习记录(二十三):自定义TableModel和Delegate(一)

Python2.7 或 Python3.7
PySide2 Version: 5.11.2
官方文档:http://doc.qt.io/qtforpython/index.html

代码:

# -*- coding:utf-8 -*-
from PySide2 import QtWidgets, QtCore, QtGui
import sys


# 自定义Delegate
class MyDelegate(QtWidgets.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtWidgets.QStyledItemDelegate.__init__(self, parent)
        self.progressbaroption = QtWidgets.QStyleOptionProgressBar()

    # 绘制特定区域中的内容,这里是进度条
    def paint(self, painter, option, index):
        # 当列为2,行为0时执行(索引从0开始)
        if index.column() == 2 and index.row() == 0:
            # adjusted为微调
            self.progressbaroption.rect = option.rect.adjusted(2, 2, -2, -2)
            self.progressbaroption.minimun = 0
            self.progressbaroption.maximum = 100
            self.progressbaroption.progress = 50
            self.progressbaroption.text = '50%'
            # 显示文本
            self.progressbaroption.textVisible = True
            
            # 绘制简单控件
            QtWidgets.QApplication.style().drawControl(
                QtWidgets.QStyle.CE_ProgressBar,
                self.progressbaroption,
                painter)
        # 否则执行默认的delegate
        else:
            QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)


# 自定义TableModel
class MyTableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self.rows = 4
        self.cols = 3
        self.d = [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]]

    # 返回table中有几行
    def rowCount(self, index):
        return self.rows

    # 返回table中有几列
    def columnCount(self, index):
        return self.cols

    # 返回model中的存储的数据,这里就是d中的值
    def data(self, index, role):
        # 如果index无效则返回0
        if not index.isValid():
            return 0
        # 如果以文本方式展现
        if role == QtCore.Qt.DisplayRole:
            return self.d[index.column()][index.row()]


app = QtWidgets.QApplication()

# 打印出支持的风格
print(QtWidgets.QStyleFactory.keys())
# 设置整个程序的风格
app.setStyle('Fusion')

window = QtWidgets.QWidget()
window.setFixedSize(500, 400)
layout = QtWidgets.QVBoxLayout(window)

tableview = QtWidgets.QTableView()

# 将所有的项都应用自定义的Delegate
tableview.setItemDelegate(MyDelegate())

# 生成自定义的TableModel
model = MyTableModel()

# 加载自定义的TableModel
tableview.setModel(model)
layout.addWidget(tableview)
window.setLayout(layout)
window.show()

sys.exit(app.exec_())

效果图:

PySide2学习记录(二十三):自定义TableModel和Delegate(一)_第1张图片
图1

注意,经过测试,在Mac上运行,并且风格设置为macintosh时,可能不能正常显示,猜测为bug,还没有找到解决办法。在Mac下,如果想要得出正常结果,建议将风格设置为Windows或Fusion,其它系统没有影响。如果不设置风格,将会自动设置为电脑系统对应的风格。

自定义TableModel时,必须要实现以下三个函数:
rowCount([parent=QModelIndex()])
返回值为int
int columnCount([parent=QModelIndex()])
返回值为int
data(index[, role=Qt.DisplayRole])
index为QModelIndex类型
返回值为object
详细信息见:QAbstractTableModel

如果想把进度条换成按钮,可以参考如下代码并删除无关的代码:

PySide2学习记录(二十三):自定义TableModel和Delegate(一)_第2张图片
图2

效果图:

PySide2学习记录(二十三):自定义TableModel和Delegate(一)_第3张图片
图3

你可能感兴趣的:(PySide2学习记录(二十三):自定义TableModel和Delegate(一))