Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python - How do I display tabular data in Treeview?

I'm trying to create a table from a Pandas dataframe which has expandable rows.

I've looked on Stack Overflow (see links below), the Qt docs and Rapid GUI Programming with Python and Qt, which is useful but 13 years out of date. I've seen a couple of similar questions asked on this (I'm still a beginner so don't understand the solutions all that well)

Display data from QAbstractTableModel in QTreeView

How can I make a table that can collapse its rows into categories in Qt?

These seem to focus on either using QAbstractTableModel or turning that into QTreeView because they have similar properties both subclassed from QAbstractItemModel (if I've understood that correctly). I'm wondering why none of them use the TreeOfTableModel like in the Rapid GUI Programming book or the article below:

https://www.pythonstudio.us/pyqt-programming/representing-tabular-data-in-trees.html

Which of these models would work best to give my table (see code below) expandable rows like treeview and what steps should I take to it work?

import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidgetItem, QMainWindow
import pandas as pd

global dict
dict = {"brand": ["Ford", "BMW", "Mercedes"], "model": ["Mustang", "7 Series", "A Class"], "year": [1964, 2002, 2020]}

global data

data = pd.DataFrame(dict, columns = ['brand', 'model','year'])

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
            super(TableModel, self).__init__()
            self._data = data

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

    def rowCount(self, index):
            return self._data.shape[0]

    def columnCount(self, index):
            return self._data.shape[1]

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

                    if orientation == Qt.Vertical:
                         return str(self._data.index[section])
            

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()

        self.page = QWidget()
        
        self.treeView = QtWidgets.QTreeView(self.page)
        self.treeView.setGeometry(QRect(0, 0, 882, 346))
        
        self.model = TableModel(data)
        self.treeView.setModel(self.model)   
        
        self.setCentralWidget(self.treeView)

app = QtWidgets.QApplication(sys.argv)
window = Ui()
window.show()
app.exec_()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...