在 PyQt5 Tableview 中解析包含列表的信号发射列表

Parsing a signal emitted List containing list inside a PyQt5 Tableview

我有一个脚本 returns 列表中的字典格式数据。所以我的列表 data[] 包含以下数据


[['https://www.impasd.com.au/', 'https://www.impasd.com.au/who-we-are/'],['697559', '459048'], ['Full Service', 'Agency Partner']]

我想在 PyQt5 Table 视图中的 Table 中显示此数据,我已将其定义为以下内容。数据实时更新,所以我想在数据更新时继续更新 table。

现在我有一个如下所示的 PyQt5 windows。大白部分定义为一个TabelView

我主要的PyQt5代码是main.py

class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self) :
        super().__init__()

        self.table = QtWidgets.QTableView()
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1327, 901)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableView = QtWidgets.QTableView(self.centralwidget)
        self.tableView.setGeometry(QtCore.QRect(0, 0, 1331, 851))
        self.tableView.setObjectName("tableView")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

data = [['https://www.impasd.com.au/', 'https://www.impasd.com.au/who-we-are/'], ['697559', '459048'], ['Full Service', 'Agency Partner']]

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

        self.setCentralWidget( self.table )

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

当进程运行时,我的数据在主 class 中可用,我还定义了一个模型

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

    def data(self, index, role):
        if role == Qt.DisplayRole:
            # See below for the nested-list data structure.
            # .row() indexes into the outer list,
            # .column() indexes into the sub-list
            return self._data[index.row()][index.column()]

    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])

并且没有显示Table。我可能做错了什么?

----更新----

我现在已将我的结果[] 转换为列表列表,因此问题略有变化。 "Parsing a signal emitted List containing LIST inside a PyQt5 Tableview"

我在过去几个小时也取得了一些进展,所以 window 触发并且我没有收到任何错误并且程序以退出代码 0 结束,但是 table 是空的?

您正在定义 QMainWindow 的两个不同实例,即 MainWindowui。其中每一个都有一个 QTableView 设置为它们的中央小部件,但您只需为其中一个设置一个模型,即 ui.centralWidget()。但是,显示的 window 是另一个:MainWindow,它没有为其 table 视图设置模型。

由于您似乎一直在编辑从 Qt Designer + pyuic 生成的 .py 文件,我建议您从 pyuic 重新生成 .py 而不是直接编辑它(这总是一个坏主意)创建一个单独的 .py 文件,在其中定义一个 class,它继承自 QMainWindowUi_MainWindow 以设置您的主要 window。因此,假设 Ui_MainWindow.py 是 Qt Designer 的输出文件,您将创建一个不同的 .py 文件并执行类似这样的操作

import QtWidgets, QtCore
import Ui_MainWindow, TableModel

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        data = [['https://www.impasd.com.au/', 'https://www.impasd.com.au/who-we-are/'], 
                ['697559', '459048'], ['Full Service', 'Agency Partner']]
        self.setupUi(self)
        self.model = TableModel(data)
        self.tableView.setModel(self.model)

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    main_window = MainWindow()
    main_window.show()
    app.exec_()