jgsedi
jgsedi

Reputation: 247

PyQt5 TableModel(QAbstractTableModel) shows no data

Hello i started with PyQt5 recently and tried to implement a csv parser:

But i struggled with the descendant of the QAbstractTableModel. No data in the view!

The tutorial from qt5 (https://doc.qt.io/qt-5/qabstracttablemodel.html) says i have to implement some methods in the TableModel - class. I think i did that but - no data in the view.

from PyQt5.QtWidgets import \
    QApplication, QWidget, QVBoxLayout, QTableView
from PyQt5.QtCore import \
    QAbstractTableModel, QVariant, Qt


class TableModel(QAbstractTableModel):
    def __init__(self):
        super().__init__()
        self._data = []

    def set(self, data):
        self._data = data

    def rowCount(self, index):
        return len(self._data)

    def columnCount(self, index):
        return len(self._data[0])

    def data(self, index, role=Qt.DisplayRole):
        if not index.isValid():
            return QVariant()
        elif role == Qt.DisplayRole:
            row = index.row()
            return self._data[row]
        else:
            return QVariant()

    def headerData(self, p_int, Qt_Orientation, role=None):
        return self._data[0]


class View(QWidget):
    def __init__(self):
        super().__init__()

        model = TableModel()
        model.set(data)

        self.tbl = QTableView(self)
        self.tbl.setModel(model)

    def setup(self):
        self.setMinimumHeight(600)
        self.setMinimumWidth(800)

        self.vlayout = QVBoxLayout(self)
        self.vlayout.addWidget(self.tbl)


def main():
    import sys
    app = QApplication(sys.argv)

    window = View()
    window.setup()
    window.show()

    sys.exit(app.exec_())

main()

Some Mock data from here:

data = [f.strip().split(",") for f in """\
    id,first_name,last_name,email,gender,ip_address
    1,Hillard,Tasseler,[email protected],Male,104.153.237.243
    2,Tyrus,Oley,[email protected],Male,163.73.24.45
    3,Kora,Covil,[email protected],Female,158.44.166.87
    4,Phineas,McEntee,[email protected],Male,71.82.246.45
    5,Dottie,Spraging,[email protected],Female,226.138.241.22
    6,Andria,Ivatts,[email protected],Female,57.5.76.78
    7,Missy,Featherstone,[email protected],Female,9.56.215.203
    8,Anstice,Sargant,[email protected],Female,36.115.185.109
    9,Teresita,Trounce,[email protected],Female,240.228.133.166
    10,Sib,Thomke,[email protected],Female,129.191.2.7
    11,Amery,Dallander,[email protected],Male,4.115.194.100
    12,Rourke,Rowswell,[email protected],Male,48.111.190.66
    13,Cloe,Benns,[email protected],Female,142.48.24.44
    14,Enos,Fery,[email protected],Male,59.19.200.235
    15,Russell,Capelen,[email protected],Male,38.205.20.141""".split()]

Upvotes: 1

Views: 1335

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

The data method must return a value as a string, not a list, in your case self._data[row] is a list so the solution is to use the column to obtain a single element from that list:

def data(self, index, role=Qt.DisplayRole):
    if not index.isValid():
        return QVariant()
    elif role == Qt.DisplayRole:
        row = index.row()
        col = index.column()
        return self._data[row][col]
    else:
        return QVariant()

Upvotes: 3

Related Questions