QHeaderView doesn't have 'ResizeToContents' attribute
-
Hi,
I'm using Qt6 with Python3 and I'm trying to create a simple app that displays a table in a window.
I've worked out how to get column headers, they must be set in the TableModel, but the column resizing seems to be set in QTableView.
I've tracked down the setSectionResizeMode() method but I cannot get it to take either of the two choices I need.
The relevant code is as follows:
self.mainTable = QTableView() # Set the column sizing options header = self.mainTable.horizontalHeader() header.setSectionResizeMode( QtWidgets.QHeaderView.ResizeToContents ) header.setSectionResizeMode( 2, QtWidgets.QHeaderView.Stretch )
but when I execute it I get:
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: type object 'QHeaderView' has no attribute 'ResizeToContents'
The error is the same of I swap 'ResizeToContents' with 'Stretch'.
All the documentation I have found for Qt6, both for Python and C++, states that the attributes I have provided are correct. I've also looked at the Python libraries on my machine where I found this in the PyQt6/QtWidgets.pyi file:
class QHeaderView(QAbstractItemView): class ResizeMode(enum.Enum): Interactive = ... # type: QHeaderView.ResizeMode Fixed = ... # type: QHeaderView.ResizeMode Stretch = ... # type: QHeaderView.ResizeMode ResizeToContents = ... # type: QHeaderView.ResizeMode Custom = ... # type: QHeaderView.ResizeMode ... def setSectionResizeMode(self, mode: 'QHeaderView.ResizeMode') -> None: ...
which suggests that what I am doing is correct.
I am at a loss to work out what I should be using. Can you provide any recommendations please?
-
Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.
-
-
Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.
@friedemannkleint said in QHeaderView doesn't have 'ResizeToContents' attribute:
Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.
Perfect, that resolved the problem. Thanks.
-
@friedemannkleint said in QHeaderView doesn't have 'ResizeToContents' attribute:
Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.
Perfect, that resolved the problem. Thanks.
@Parthe
Be aware: it's not just this one, it's (more or less) every Qtenum
which has changed to be fully qualified at Qt6. In both PySide6 & PyQt6. And they have not changed the documentation, sadly.For this one I think you look at the doc page at https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QHeaderView.html#PySide6.QtWidgets.PySide6.QtWidgets.QHeaderView.ResizeMode. There you see:
PySide6.QtWidgets.QHeaderView.ResizeMode
QHeaderView
will automatically resize....QHeaderView.ResizeToContents
I think you have to put these two together: so it actually needs to be
....QHeaderView.ResizeMode.ResizeToContents
. And I guess for other enums similarly.Quite a while ago there was a guy who posted and had written some "conversion" program, or you could look at its source, for all the enum changes. But I can't recall where to find it.[*]
See also https://doc.qt.io/qtforpython-6/considerations.html#the-differences-between-old-and-new-enums, e.g.
Qt.AlignLeft
becomesQt.AlignmentFlag.AlignLeft
[*] Ah, found it! The accepted solution at https://stackoverflow.com/questions/72086632/migrating-to-qt6-pyqt6-what-are-all-the-deprecated-short-form-names-in-qt5
I wrote a script to extract all the short-form and corresponding fully qualified enum names from the PyQt6 installation. It then does the conversions automatically:
You can probably just look at the source code and adjust manually if you don't want to run his whole script.