Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to resize QWidget to fit its contents?
Forum Updated to NodeBB v4.3 + New Features

How to resize QWidget to fit its contents?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 7.3k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lachdanan
    wrote on last edited by
    #1

    Hi,

    Basically I have a QTableWidget inside a QVBoxLayout. I set everything so that my table widget resizes to fit the contents. This is good, but I can't get my QWidget that hosts this table widget to resize itself to show everything.

    I checked tablewidget.size() but the size is not the correct size, because if I use the same size for my QWidget, the table is cropped, i.e. I can't see it fully.

    How can I achieve this?

    Thanks in advance.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      try tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      L 1 Reply Last reply
      2
      • VRoninV VRonin

        try tableWidget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

        L Offline
        L Offline
        lachdanan
        wrote on last edited by lachdanan
        #3

        @VRonin Thanks a lot, you are right it works :) I have another issue about the row heights, I am trying to resize them but they don't seem to get smaller in height than this:

        alt text

        I am doing it like this:
        ```
        hh = tableWidget.horizontalHeader()
        hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

            hh.setFixedHeight(50)
            #hh.setStretchLastSection(True)
        
            vh = tableWidget.verticalHeader()
            vh.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
            vh.setDefaultSectionSize(1)
        
        
        I don't understand why row heights refuse to get smaller.
        mrjjM 1 Reply Last reply
        0
        • L lachdanan

          @VRonin Thanks a lot, you are right it works :) I have another issue about the row heights, I am trying to resize them but they don't seem to get smaller in height than this:

          alt text

          I am doing it like this:
          ```
          hh = tableWidget.horizontalHeader()
          hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

              hh.setFixedHeight(50)
              #hh.setStretchLastSection(True)
          
              vh = tableWidget.verticalHeader()
              vh.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
              vh.setDefaultSectionSize(1)
          
          
          I don't understand why row heights refuse to get smaller.
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @lachdanan
          Hi
          There is
          vh.setMinimumSectionSize(xx);
          and setDefaultSectionSize won't go under that value
          so you must lower that first to go really low.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lachdanan
            wrote on last edited by
            #5

            @mrjj said in How to resize QWidget to fit its contents?:

            setMinimumSectionSize

            Thanks a lot I just tried that but no luck, and if I remove default section size function call, then they get larger:

            vh.setDefaultSectionSize(1)
            vh.setMinimumSectionSize(1)

            This still looks same size for example.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lachdanan
              wrote on last edited by
              #6

              When I used this it worked: vh.setSectionResizeMode(QtWidgets.QHeaderView.Stretch)

              1 Reply Last reply
              1

              • Login

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