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

QTableWidget automatically sizes rows too large

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 6.8k 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 Publicnamer

    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?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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
      • JonBJ JonB

        @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 last edited by
        #4

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

        JoeCFDJ 1 Reply Last reply
        0
        • P Publicnamer

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

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on 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
          0
          • JoeCFDJ JoeCFD

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

            JoeCFDJ 1 Reply Last reply
            0
            • P Publicnamer

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

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

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

              P 2 Replies Last reply
              0
              • JoeCFDJ JoeCFD

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

                P Offline
                P Offline
                Publicnamer
                wrote on 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.
                JonBJ 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

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

                  P Offline
                  P Offline
                  Publicnamer
                  wrote on 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.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • P Publicnamer

                    @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.
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    0
                    • JonBJ JonB

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

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

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on 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
                        0
                        • JoeCFDJ JoeCFD

                          @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 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
                          • JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on 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
                            0
                            • JoeCFDJ JoeCFD

                              @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 last edited by
                              #15

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

                              JoeCFDJ 1 Reply Last reply
                              0
                              • P Publicnamer

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

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

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

                                P 1 Reply Last reply
                                0
                                • JoeCFDJ JoeCFD

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

                                  P Offline
                                  P Offline
                                  Publicnamer
                                  wrote on 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.

                                  JoeCFDJ 1 Reply Last reply
                                  0
                                  • P Publicnamer

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

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

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

                                    P 1 Reply Last reply
                                    0
                                    • JoeCFDJ JoeCFD

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

                                      P Offline
                                      P Offline
                                      Publicnamer
                                      wrote on 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
                                      0
                                      • P Publicnamer

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

                                        • Login

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