Table View

Listing 12 scripts/PYQT3/03_data_model/01_table_view/main.py
# -*- coding: utf-8 -*-
 import sys
 from PyQt5.QtCore import Qt, QAbstractTableModel
 from PyQt5.QtWidgets import QApplication, QTableView, QMainWindow, QVBoxLayout, QWidget

 class TableModel(QAbstractTableModel):
     def __init__(self, data):
         super(TableModel, self).__init__()
         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):
         if role == Qt.DisplayRole:
             return self._data[index.row()][index.column()]

     def flags(self, index):
         return super(TableModel, self).flags(index) & ~Qt.ItemIsEditable

 class MainWindow(QMainWindow):
     def __init__(self):
         super(MainWindow, self).__init__()

         self.table = QTableView()

         data = [
             [1, "김", "철수"],
             [2, "박", "혁수"],
             [3, "이", "고수"],
         ]

         self.model = TableModel(data)
         self.table.setModel(self.model)

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

 if __name__ == '__main__':
     app = QApplication(sys.argv)
     window = MainWindow()
     window.show()
     sys.exit(app.exec_())
  • The QTableView and QAbstractTableModel are employed to display table data.

  • The TableModel class inherits from QAbstractTableModel.

    • The rowCount and columnCount methods return the number of rows and columns in the table respectively.

    • The data method returns data from a specific cell.

    • The flags method ensures items are set to non-editable.

data = [
    [1, "김", "철수"],
    [2, "박", "혁수"],
    [3, "이", "고수"],
]

The above data is displayed in the table view through the TableModel.