1

PyQt6:在 Win10 上,垂直标题只有行号没有行。在 Linux 上,KDE 两者都有。

我错过了什么或错误吗?我无法在 MAC 上进行测试。

赢10

import sys

from PyQt6.QtWidgets import QApplication, QMainWindow, QTableView
from PyQt6.QtCore import Qt, QAbstractTableModel

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

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

    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The following takes the first sub-list, and returns
        # the length (only works if all rows are an equal length)
        return len(self._data[0])

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

        self.table = QTableView()

        data = [[7,  9,  2],
                [1, -1, -1],
                [3,  5.3, -5],
                [3,  3,  2],
                [7,  8, 23],]

        self.model = TableModel(data)

        self.table.setModel(self.model)

        self.setCentralWidget(self.table)

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
4

0 回答 0