If I try to get the signal when my tableview changes, Python raises this error:
Traceback (most recent call last):
File "UIreadresultwindow.py", line 361, in <module>
ui.setupUi(ReadResultWindow)
File "UIreadresultwindow.py", line 113, in setupUi
self.tableEntity.selectionModel().selectionChanged.connect(self.change
_display_result)
AttributeError: 'NoneType' object has no attribute 'selectionChanged'
I define tableEntity as:
self.tableEntity = QtWidgets.QTableView(self.centralWidget)
Edit: At first my QTableView is empty. I have to open a file to fill it.
Edit2: To be more specific, I have something like this:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ReadResultWindow(object):
def setupUi(self, ReadResultWindow):
ReadResultWindow.setObjectName("ReadResultWindow")
ReadResultWindow.resize(661, 438)
self.centralWidget = QtWidgets.QWidget(ReadResultWindow)
self.centralWidget.setObjectName("centralWidget")
self.tableEntity = QtWidgets.QTableView(self.centralWidget)
self.tableEntity.setObjectName("tableEntity")
self.Open = QtWidgets.QPushButton(self.centralWidget)
self.Open.setObjectName("Open")
self.Open.clicked.connect(self.on_open_file)
self.tableEntity.selectionModel().selectionChanged.connect(self.change_display_result)
def on_open_file(self):
x=[1,2,3,4]
self.model= QtGui.QStandardItemModel()
for item in x:
self.model.invisibleRootItem().appendRow(
QtGui.QStandardItem(str(x)))
self.proxy= QtCore.QSortFilterProxyModel()
self.proxy.setSourceModel(self.model)
self.tableEntity.setModel(self.proxy)
self.tableEntity.resizeColumnsToContents()
def change_display_result(self,selected,deselected):
index_entity = self.tableEntity.selectionModel().selectedIndexes()
temp_entity = self.tableEntity.selectionModel().model()
for index in sorted(index_entity):
print(str(temp_entity.data(index)))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ReadResultWindow = QtWidgets.QMainWindow()
ui = Ui_ReadResultWindow()
ui.setupUi(ReadResultWindow)
ReadResultWindow.show()
sys.exit(app.exec_())
The reason why you get that error is that you did not set the model on the table before trying to access the selection-model. The best way to fix this is to move the model setup code out of on_open_file
and into setupUi
. The on_open_file
then just needs to clear the model before reloading the data.
Below is a re-write of your example. Note that I had to fix quite a few other things to get it to work (mainly the central-widget and layout).
import sys, random
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ReadResultWindow(object):
def setupUi(self, ReadResultWindow):
ReadResultWindow.resize(661, 438)
self.tableEntity = QtWidgets.QTableView()
self.model = QtGui.QStandardItemModel()
self.proxy = QtCore.QSortFilterProxyModel()
self.proxy.setSourceModel(self.model)
self.tableEntity.setModel(self.proxy)
self.tableEntity.selectionModel().selectionChanged.connect(
self.change_display_result)
self.Open = QtWidgets.QPushButton('Test')
self.Open.clicked.connect(self.on_open_file)
widget = QtWidgets.QWidget(ReadResultWindow)
layout = QtWidgets.QVBoxLayout(widget)
layout.addWidget(self.tableEntity)
layout.addWidget(self.Open)
ReadResultWindow.setCentralWidget(widget)
def on_open_file(self):
self.model.setRowCount(0)
x = random.sample(range(10, 100), 10)
for item in x:
self.model.invisibleRootItem().appendRow(
QtGui.QStandardItem(str(item)))
self.tableEntity.resizeColumnsToContents()
def change_display_result(self,selected,deselected):
index_entity = self.tableEntity.selectionModel().selectedIndexes()
temp_entity = self.tableEntity.selectionModel().model()
for index in sorted(index_entity):
print(str(temp_entity.data(index)))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ReadResultWindow = QtWidgets.QMainWindow()
ui = Ui_ReadResultWindow()
ui.setupUi(ReadResultWindow)
ReadResultWindow.show()
sys.exit(app.exec_())