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?

How to resize QWidget to fit its contents?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 7.2k 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.
  • L Offline
    L Offline
    lachdanan
    wrote on 27 Feb 2020, 14:45 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
    • V Offline
      V Offline
      VRonin
      wrote on 27 Feb 2020, 14:47 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 27 Feb 2020, 15:07
      2
      • V VRonin
        27 Feb 2020, 14:47

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

        L Offline
        L Offline
        lachdanan
        wrote on 27 Feb 2020, 15:07 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.
        M 1 Reply Last reply 27 Feb 2020, 19:21
        0
        • L lachdanan
          27 Feb 2020, 15:07

          @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.
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Feb 2020, 19:21 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 28 Feb 2020, 04:52 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 28 Feb 2020, 06:38 last edited by
              #6

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

              1 Reply Last reply
              1

              4/6

              27 Feb 2020, 19:21

              • Login

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