pyqt5. Regular table:
from PyQt5 import QtWidgets
app = QtWidgets.QApplication([])
table = QtWidgets.QTableWidget(3, 3)
table.setHorizontalHeaderLabels(['1', '2', '3'])
table.show()
app.exec_()
Here, clicking on column header selects the whole column.
If I try to pass explicitly the QtWidgets.QHeaderView, I lose some of the default behavior, notably clicking on header no longer selects the column:
from PyQt5 import QtWidgets, QtCore
class CustomTableWidget(QtWidgets.QTableWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setHorizontalHeader(QtWidgets.QHeaderView(QtCore.Qt.Horizontal))
app = QtWidgets.QApplication([])
table = CustomTableWidget(3, 3)
table.setHorizontalHeaderLabels(['1', '2', '3'])
table.show()
app.exec_()
I tried selecting different parent widgets for QHeaderView including the table, subclassing QHeaderView, etc. Could not achieve default behavior. (I obviously wouldn't want to reimplement explicitly the defaults from scatch)
You have a normal table, and when trying to subclass it you lose the default behavior of being able to select columns ?
That will be since you added the line
self.setHorizontalHeader(QtWidgets.QHeaderView(QtCore.Qt.Horizontal)). When you make a declaration like this its going to override any default behavior inbuilt. Removing it will give your expected result.I made thin as a PyQT6 version as its most recent, but do change anyway you want. Hope that helps.