I have a script that returns data in a dict format inside a list. So my list data[] contains the following data
[['https://www.impasd.com.au/', 'https://www.impasd.com.au/who-we-are/'],['697559', '459048'], ['Full Service', 'Agency Partner']]
I would like to display this data inside a Table in the PyQt5 TableView that I have defined as folow. The data updates live so I had like to continue to update the table as the data updates.
Now I have a PyQt5 windows that looks like below. The big white section is defined as a TabelView
My main PyQt5 code is 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_())
When the process runs my data is available within the main class where I have also defined a Model
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])
And no Table is displayed. What could I be doing wrong?
----UPDATE----
I have now converted my results[] into a list of lists so the question has changed slightly. "Parsing a signal emitted List containing LIST inside a PyQt5 Tableview"
I have also made some progress in the last few hours so the window triggers and I get no error and the program finishes with exit code 0, yet the table is empty?
You are defining two different instances of QMainWindow
namely MainWindow
and ui
. Each of these has a QTableView
set as their central widget but you only set a model for one of them namely ui.centralWidget()
. However, the window that is shown is the other one: MainWindow
, which doesn't have a model set for its table view.
Since you appear to have been editing a.py file which was generated from Qt Designer + pyuic, I would suggest that you regenerate the.py from pyuic and instead of editing it directly (which is always a bad idea) create a separate.py file where you define a class which inherits from both QMainWindow
and Ui_MainWindow
to setup your main window. So, assuming that Ui_MainWindow.py
is the output file of Qt Designer, you would create a different.py file and do something like this
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_()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.