How do I attach a listener to PyQT's TableView's select all button?
PyQT TableView Sample
I have tried using below which only gets triggered when a cell is selected.
table.clicked.connect()
I have also tried adding listener to both header which only gets triggered when the columns or rows are selected
table.horizontalHeader().sectionClicked.connect()
table.verticalHeader().sectionClicked.connect()
They are all working as what they are intended to do, however I want to add listener to the top left button, the select all button. I can't seem to find from Qt's documentation that mentions this particular part of the button.
Code:
# Imported from Qt Designer created dialog
dialog = x.UI_Dialog()
table = dialog.tableSchedule
table.setModel(___) # I used my custom model that inherits from QtCore.QAbstractTableModel
table.clicked.connect(lambda: print('cell'))
table.horizontalHeader().sectionClicked.connect(lambda: print('col'))
table.verticalHeader().sectionClicked.connect(lambda: print('row'))
Answer (by @eyllanesc): Get the corner button of the QTableView and add listener to it.
corner = table.findChild(QAbstractButton)
corner.clicked.connect(___)
Upon further inspection by getting the children of QTableView, these are the components:
That element of the corner is a QAbstractButton
and there is no direct method to obtain it, a possible solution is to use findChild()
:
corner = table.findChild(QAbstractButton)
Example:
if __name__ == "__main__":
app = QApplication(sys.argv)
table = QTableView()
corner = table.findChild(QAbstractButton)
table.clicked.connect(lambda: print('cell'))
table.horizontalHeader().sectionClicked.connect(lambda: print('col'))
table.verticalHeader().sectionClicked.connect(lambda: print('row'))
corner.clicked.connect(lambda: print('corner'))
model = QStandardItemModel(3, 4, table)
table.setModel(model)
table.show()
sys.exit(app.exec_())