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. QTableWidget automatically sizes rows too large

QTableWidget automatically sizes rows too large

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 6.3k 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
    Publicnamer
    wrote on 18 Nov 2021, 23:29 last edited by
    #1

    I'm trying to tell QTableWidget to always size each row to be the exact
    height of that row's contents. I am using ResizeToContents:

    table->setWordWrap (true);
    ...
    QHeaderView *verticalHeader = mytable->verticalHeader();
    verticalHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
    

    However what I am seeing is that rows are 2-3 times taller than they need to be.
    I'm also using spans to combine columns.
    Is the above the correct way to auto-size rows?

    J 1 Reply Last reply 19 Nov 2021, 07:53
    0
    • P Publicnamer
      19 Nov 2021, 19:44

      @JoeCFD
      OK what is a section in this context?

      Also, why would a call to table->setRowHeight(row, height) have no effect?

      P Offline
      P Offline
      Publicnamer
      wrote on 19 Nov 2021, 19:56 last edited by
      #20

      @Publicnamer
      OK I believe I found the bug.

      At the end of my function which fills the table with data, I had a call to:

       void QTableView::resizeRowsToContents()
      

      This was overriding all of my attempts to programmatically set the ideal row sizes but more importantly it was setting bogus row heights.

      1 Reply Last reply
      1
      • P Publicnamer
        18 Nov 2021, 23:29

        I'm trying to tell QTableWidget to always size each row to be the exact
        height of that row's contents. I am using ResizeToContents:

        table->setWordWrap (true);
        ...
        QHeaderView *verticalHeader = mytable->verticalHeader();
        verticalHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
        

        However what I am seeing is that rows are 2-3 times taller than they need to be.
        I'm also using spans to combine columns.
        Is the above the correct way to auto-size rows?

        J Offline
        J Offline
        JonB
        wrote on 19 Nov 2021, 07:53 last edited by JonB
        #2

        @Publicnamer
        You seem to be resizing the vertical header, which means the column widths. If you want "height of that row's contents" then, not that I've ever used it, wouldn't that require resizing the horizontal header?

        EDIT Whoops, have I mixed up which is horizontal versus vertical?!

        P 1 Reply Last reply 19 Nov 2021, 15:05
        0
        • V Offline
          V Offline
          VRonin
          wrote on 19 Nov 2021, 10:43 last edited by
          #3

          Do you have custom views/delegates?

          The height of each row is controlled by QAbstractItemView::sizeHintForRow

          "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

          1 Reply Last reply
          2
          • J JonB
            19 Nov 2021, 07:53

            @Publicnamer
            You seem to be resizing the vertical header, which means the column widths. If you want "height of that row's contents" then, not that I've ever used it, wouldn't that require resizing the horizontal header?

            EDIT Whoops, have I mixed up which is horizontal versus vertical?!

            P Offline
            P Offline
            Publicnamer
            wrote on 19 Nov 2021, 15:05 last edited by
            #4

            @JonB Using the horizontal header causes the column widths to be affected. Nothing ever wraps.

            J 1 Reply Last reply 19 Nov 2021, 15:15
            0
            • P Publicnamer
              19 Nov 2021, 15:05

              @JonB Using the horizontal header causes the column widths to be affected. Nothing ever wraps.

              J Offline
              J Offline
              JoeCFD
              wrote on 19 Nov 2021, 15:15 last edited by JoeCFD
              #5

              @Publicnamer It could be tricky. Find the font size(if there are only strings) or image height and then set row minimum and maximum height(slightly bigger than font size) to be the same.

              P 1 Reply Last reply 19 Nov 2021, 15:28
              0
              • J JoeCFD
                19 Nov 2021, 15:15

                @Publicnamer It could be tricky. Find the font size(if there are only strings) or image height and then set row minimum and maximum height(slightly bigger than font size) to be the same.

                P Offline
                P Offline
                Publicnamer
                wrote on 19 Nov 2021, 15:28 last edited by Publicnamer
                #6

                @JoeCFD What is the point of resizing automatically for content size, if I have to specify the row height?
                This is seeming like a broken feature.
                Also, table widget doesn't have a setRowMinimumHeight or setRowMaximumHeight method. It only has setRowHeight.
                Whether I call setRowHeight or not, I get the same result.

                J 1 Reply Last reply 19 Nov 2021, 15:38
                0
                • P Publicnamer
                  19 Nov 2021, 15:28

                  @JoeCFD What is the point of resizing automatically for content size, if I have to specify the row height?
                  This is seeming like a broken feature.
                  Also, table widget doesn't have a setRowMinimumHeight or setRowMaximumHeight method. It only has setRowHeight.
                  Whether I call setRowHeight or not, I get the same result.

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 19 Nov 2021, 15:38 last edited by
                  #7

                  @Publicnamer
                  do this in vertical header
                  void setMaximumSectionSize(int size)
                  void setMinimumSectionSize(int size)

                  P 2 Replies Last reply 19 Nov 2021, 18:47
                  0
                  • J JoeCFD
                    19 Nov 2021, 15:38

                    @Publicnamer
                    do this in vertical header
                    void setMaximumSectionSize(int size)
                    void setMinimumSectionSize(int size)

                    P Offline
                    P Offline
                    Publicnamer
                    wrote on 19 Nov 2021, 18:47 last edited by
                    #8

                    @JoeCFD BTW I've noticed the table widget is setting the wrong row height in a systematic way:

                    • If a row has 1 word in it, the vertical space given to it is 1 line.
                    • If a row has 2 words in it, the vertical space given to it is 2 lines.
                    • If a row has 3 words in it, the vertical space given to it is 3 lines.
                    • If a row has 4 words in it, the vertical space given to it is 4 lines.
                      Etc.
                    J 1 Reply Last reply 19 Nov 2021, 18:55
                    0
                    • J JoeCFD
                      19 Nov 2021, 15:38

                      @Publicnamer
                      do this in vertical header
                      void setMaximumSectionSize(int size)
                      void setMinimumSectionSize(int size)

                      P Offline
                      P Offline
                      Publicnamer
                      wrote on 19 Nov 2021, 18:49 last edited by
                      #9

                      @JoeCFD said in QTableWidget automatically sizes rows too large:

                      setMaximumSectionSize

                      OK I did that but it doesn't help. I need to have row heights that adapt to the contents. The contents may have different fonts, so they can't all be the same. I need the text to wrap.

                      J 1 Reply Last reply 19 Nov 2021, 19:26
                      0
                      • P Publicnamer
                        19 Nov 2021, 18:47

                        @JoeCFD BTW I've noticed the table widget is setting the wrong row height in a systematic way:

                        • If a row has 1 word in it, the vertical space given to it is 1 line.
                        • If a row has 2 words in it, the vertical space given to it is 2 lines.
                        • If a row has 3 words in it, the vertical space given to it is 3 lines.
                        • If a row has 4 words in it, the vertical space given to it is 4 lines.
                          Etc.
                        J Offline
                        J Offline
                        JonB
                        wrote on 19 Nov 2021, 18:55 last edited by JonB
                        #10

                        @Publicnamer said in QTableWidget automatically sizes rows too large:

                        If a row has 1 word in it, the vertical space given to it is 1 line.
                        If a row has 2 words in it, the vertical space given to it is 2 lines.
                        If a row has 3 words in it, the vertical space given to it is 3 lines.
                        ...

                        I would try to diagnose this. If you (temporarily) set word wrap off, does that affect? More likely, can you (temporarily) switch off any column spanning, does that affect? And are you able to post a screen shot of what it looks like with, say, 3 rows of 1, 2 and 3 words respectively?

                        P 1 Reply Last reply 19 Nov 2021, 19:25
                        0
                        • J JonB
                          19 Nov 2021, 18:55

                          @Publicnamer said in QTableWidget automatically sizes rows too large:

                          If a row has 1 word in it, the vertical space given to it is 1 line.
                          If a row has 2 words in it, the vertical space given to it is 2 lines.
                          If a row has 3 words in it, the vertical space given to it is 3 lines.
                          ...

                          I would try to diagnose this. If you (temporarily) set word wrap off, does that affect? More likely, can you (temporarily) switch off any column spanning, does that affect? And are you able to post a screen shot of what it looks like with, say, 3 rows of 1, 2 and 3 words respectively?

                          P Offline
                          P Offline
                          Publicnamer
                          wrote on 19 Nov 2021, 19:25 last edited by Publicnamer
                          #11

                          @JonB
                          Turning off word wrap has no effect.

                          I decided to calculate my own row heights but this appears pointless too.
                          I tried changing the order in which I'm doing things e.g. setting the spans before/after the row heights, setting the text before/after setting the row heights.

                          When I store correct row heights into the tablewidget with setRowHeight there is no effect. That's even when I do this:

                           verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
                          

                          It seems as though using spans causes some buggy code within qtablewidget to take over and it becomes unresponsive to a lot of calls.

                          1 Reply Last reply
                          0
                          • P Publicnamer
                            19 Nov 2021, 18:49

                            @JoeCFD said in QTableWidget automatically sizes rows too large:

                            setMaximumSectionSize

                            OK I did that but it doesn't help. I need to have row heights that adapt to the contents. The contents may have different fonts, so they can't all be the same. I need the text to wrap.

                            J Offline
                            J Offline
                            JoeCFD
                            wrote on 19 Nov 2021, 19:26 last edited by
                            #12

                            @Publicnamer You can basically set the same font to each item. Then you will will not have any problem.

                            P 1 Reply Last reply 19 Nov 2021, 19:28
                            0
                            • J JoeCFD
                              19 Nov 2021, 19:26

                              @Publicnamer You can basically set the same font to each item. Then you will will not have any problem.

                              P Offline
                              P Offline
                              Publicnamer
                              wrote on 19 Nov 2021, 19:28 last edited by Publicnamer
                              #13

                              @JoeCFD said in QTableWidget automatically sizes rows too large:

                              You can basically set the same font to each item

                              I'm already doing that. I tried just now not setting the font and the bug persists i.e. each Space character in my strings is treated as a Newline, so a four-word string causes a row height equivalent to 4 lines. And yet the Space characters do not cause line breaks when the strings are drawn.

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                JoeCFD
                                wrote on 19 Nov 2021, 19:29 last edited by
                                #14

                                @JoeCFD said in QTableWidget automatically sizes rows too large:

                                setMaximumSectionSize

                                this is what I did in my code.
                                m_tableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::Fixed );
                                m_tableWidget->verticalHeader()->setMinimumSectionSize( item_height );
                                m_tableWidget->verticalHeader()->setMaximumSectionSize( item_height );
                                m_tableWidget->verticalHeader()->setDefaultSectionSize( item_height );

                                P 1 Reply Last reply 19 Nov 2021, 19:30
                                0
                                • J JoeCFD
                                  19 Nov 2021, 19:29

                                  @JoeCFD said in QTableWidget automatically sizes rows too large:

                                  setMaximumSectionSize

                                  this is what I did in my code.
                                  m_tableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::Fixed );
                                  m_tableWidget->verticalHeader()->setMinimumSectionSize( item_height );
                                  m_tableWidget->verticalHeader()->setMaximumSectionSize( item_height );
                                  m_tableWidget->verticalHeader()->setDefaultSectionSize( item_height );

                                  P Offline
                                  P Offline
                                  Publicnamer
                                  wrote on 19 Nov 2021, 19:30 last edited by
                                  #15

                                  @JoeCFD OK but each of my rows can be a different height, so that doesn't help me.

                                  J 1 Reply Last reply 19 Nov 2021, 19:32
                                  0
                                  • P Publicnamer
                                    19 Nov 2021, 19:30

                                    @JoeCFD OK but each of my rows can be a different height, so that doesn't help me.

                                    J Offline
                                    J Offline
                                    JoeCFD
                                    wrote on 19 Nov 2021, 19:32 last edited by
                                    #16

                                    @Publicnamer
                                    try this
                                    m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size)

                                    P 1 Reply Last reply 19 Nov 2021, 19:37
                                    0
                                    • J JoeCFD
                                      19 Nov 2021, 19:32

                                      @Publicnamer
                                      try this
                                      m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size)

                                      P Offline
                                      P Offline
                                      Publicnamer
                                      wrote on 19 Nov 2021, 19:37 last edited by
                                      #17

                                      @JoeCFD said in QTableWidget automatically sizes rows too large:

                                      m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size)

                                      I tried replacing setRowHeight with that but it doesn't work either :

                                       verticalHeader->resizeSection(myRow, myRowHeight);
                                      

                                      AFAIK I'm only dealing with rows, not sections.

                                      J 1 Reply Last reply 19 Nov 2021, 19:39
                                      0
                                      • P Publicnamer
                                        19 Nov 2021, 19:37

                                        @JoeCFD said in QTableWidget automatically sizes rows too large:

                                        m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size)

                                        I tried replacing setRowHeight with that but it doesn't work either :

                                         verticalHeader->resizeSection(myRow, myRowHeight);
                                        

                                        AFAIK I'm only dealing with rows, not sections.

                                        J Offline
                                        J Offline
                                        JoeCFD
                                        wrote on 19 Nov 2021, 19:39 last edited by JoeCFD
                                        #18

                                        @Publicnamer
                                        horizontal header section defines column width.
                                        vertical header section defines row height.

                                        P 1 Reply Last reply 19 Nov 2021, 19:44
                                        0
                                        • J JoeCFD
                                          19 Nov 2021, 19:39

                                          @Publicnamer
                                          horizontal header section defines column width.
                                          vertical header section defines row height.

                                          P Offline
                                          P Offline
                                          Publicnamer
                                          wrote on 19 Nov 2021, 19:44 last edited by Publicnamer
                                          #19

                                          @JoeCFD
                                          OK what is a section in this context?

                                          Also, why would a call to table->setRowHeight(row, height) have no effect?

                                          P 1 Reply Last reply 19 Nov 2021, 19:56
                                          0
                                          • P Publicnamer
                                            19 Nov 2021, 19:44

                                            @JoeCFD
                                            OK what is a section in this context?

                                            Also, why would a call to table->setRowHeight(row, height) have no effect?

                                            P Offline
                                            P Offline
                                            Publicnamer
                                            wrote on 19 Nov 2021, 19:56 last edited by
                                            #20

                                            @Publicnamer
                                            OK I believe I found the bug.

                                            At the end of my function which fills the table with data, I had a call to:

                                             void QTableView::resizeRowsToContents()
                                            

                                            This was overriding all of my attempts to programmatically set the ideal row sizes but more importantly it was setting bogus row heights.

                                            1 Reply Last reply
                                            1

                                            3/20

                                            19 Nov 2021, 10:43

                                            17 unread
                                            • Login

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