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. Stretch QTableWidget columns.
Forum Updated to NodeBB v4.3 + New Features

Stretch QTableWidget columns.

Scheduled Pinned Locked Moved Unsolved Qt for Python
8 Posts 3 Posters 8.5k 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.
  • J Offline
    J Offline
    Jason Olson
    wrote on last edited by
    #1

    I have a question on how to resize a QTableWidget's header and columns. The table is part of a QWidget that is using a grid layout. I have been able to get the column widths to resize using resizeColumnsToContents(), and the table itself does adjust when you adjust the window, but the headers don't adjust. I tried using horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch), which does adjust the header sizes, but the contents in the columns themselves are squashed. The only way I could get everything to resize correctly was to maximize the window. I don't want the window to be maximized. So is there a way to both size the columns with their contents, and get the headers to also adjust? Or should I just get the size of the QTableWidget after it's been populated, and then resize the window's width based on that?

    1 Reply Last reply
    1
    • Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      Good morning @Jason-Olson,
      I am not sure if I understand the difference between
      a) resizing column width (-> resized to fit contents)
      b) adjusting headers (their width always corresponds to the column width)
      With the table widget placed in a grid layout, the available space and its resize behavior can be controlled from the grid layout.
      Without seeing any code, it's hard to guess how this can be done.
      Check here and here for more information about (grid) layouts.
      Cheers
      Axel

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jason Olson
        wrote on last edited by
        #3

        I'm going to attach some pictures so I can better explain what is going on.

        fit columns to contents.png

        In this image you can see that the columns have been resized to fit the contents in them, but you'll notice that the headers didn't stretch or adjust their size. If you resize the window the following happens.

        QTableWidget did not resize.png

        Now if you tell the headers to stretch, the following happens.

        header stretch.png

        The headers stretch, and the window does resize correctly, but the contents of the columns does not resize.

        What I'm asking is how to get the columns to size based on their contents, and allow the headers to stretch as well.

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @Jason-Olson said in Stretch QTableWidget columns.:

          horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)

          can you try?
          horizontalHeader().setSectionResizeMode(qtw.QHeaderView.ResizeToContents)

          J 1 Reply Last reply
          2
          • JoeCFDJ JoeCFD

            @Jason-Olson said in Stretch QTableWidget columns.:

            horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)

            can you try?
            horizontalHeader().setSectionResizeMode(qtw.QHeaderView.ResizeToContents)

            J Offline
            J Offline
            Jason Olson
            wrote on last edited by
            #5

            @JoeCFD When I do what you have suggested I get resized columns,

            Screenshot_20221206_121743.png

            But if you resize the window, the table widget does not adjust.

            Screenshot_20221206_121819.png

            JoeCFDJ 1 Reply Last reply
            0
            • J Jason Olson

              @JoeCFD When I do what you have suggested I get resized columns,

              Screenshot_20221206_121743.png

              But if you resize the window, the table widget does not adjust.

              Screenshot_20221206_121819.png

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @Jason-Olson I think the problem is caused by global font size change. You may set your own font size for this specific widget. The font size has to be adjusted when the widget is resized.

              for your first picture, you may add
              m_tableWidget->horizontalHeader()->setStretchLastSection( true );
              to stretch the last column only in case of not fitting well.

              J 2 Replies Last reply
              1
              • JoeCFDJ JoeCFD

                @Jason-Olson I think the problem is caused by global font size change. You may set your own font size for this specific widget. The font size has to be adjusted when the widget is resized.

                for your first picture, you may add
                m_tableWidget->horizontalHeader()->setStretchLastSection( true );
                to stretch the last column only in case of not fitting well.

                J Offline
                J Offline
                Jason Olson
                wrote on last edited by
                #7

                @JoeCFD I’ll test this in the morning and see how this looks.

                1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @Jason-Olson I think the problem is caused by global font size change. You may set your own font size for this specific widget. The font size has to be adjusted when the widget is resized.

                  for your first picture, you may add
                  m_tableWidget->horizontalHeader()->setStretchLastSection( true );
                  to stretch the last column only in case of not fitting well.

                  J Offline
                  J Offline
                  Jason Olson
                  wrote on last edited by
                  #8

                  @JoeCFD This does adjust the last column, but the rest of the columns are still squished. Like Full_Name or the address fields.

                  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