Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QHeaderView doesn't have 'ResizeToContents' attribute
Forum Updated to NodeBB v4.3 + New Features

QHeaderView doesn't have 'ResizeToContents' attribute

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Parthe
    wrote on last edited by Parthe
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      friedemannkleint
      wrote on last edited by
      #2

      Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.

      P 1 Reply Last reply
      2
      • P Parthe has marked this topic as solved on
      • F friedemannkleint

        Try QHeaderView.ResizeMode.ResizeToContents . Enums need to be fully qualified now.

        P Offline
        P Offline
        Parthe
        wrote on last edited by Parthe
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • P Parthe

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Parthe
          Be aware: it's not just this one, it's (more or less) every Qt enum 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 becomes Qt.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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved