pyside6 QtQuick显示pandas dataframe

本文介绍了如何使用Pyside的pandasModel类自定义表格模型,以及如何在QML中创建TableView并与Python后端通过槽函数实现数据文件路径读取并实时刷新。讨论了如何设置TableView的大小和填充父元素的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是pyside自定义模型

class pandasModel(QAbstractTableModel):
    def __init__(self, data):
        QAbstractTableModel.__init__(self)
        self._data = data

    def rowCount(self, parent=None):
        return self._data.shape[0]

    def columnCount(self, parnet=None):
        return self._data.shape[1]

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self._data.iloc[index.row(), index.column()])
        return None

    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self._data.columns[col]
        return None

槽函数这里这么写
返回值为QAbstractItemModel
因为tableview无法接收QAbstractTableModel
不过QAbstractTableModel继承自QAbstractItemModel

@Slot(str,result=QAbstractItemModel)
def getfilepath(self,filepath):
    self.datafilepath =  filepath
    df = ppdd.readdatafile(filepath)
    self.pandasmodel = pandasModel(df)
    return self.pandasmodel

QML创建一个Rectangle,里面放TableView,要不然不显示
implicitWidth: 100
implicitHeight: 30
也必须定义,不然不显示
(这个地方应该有其他更好的方案,不能光写TableView)

Rectangle {
    width: 400
    height: 400
    color: "transparent"
    TableView {
        id:table_view
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        clip: true
        model: null
        delegate: Rectangle {
            implicitWidth: 100
            implicitHeight: 30
            Text {
                text: display
            }
        }
    }
    

然后用槽函数给他model定义一下就好了,TableView是实时刷新的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值