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. Adjust QTableView Height to its content (few lines)

Adjust QTableView Height to its content (few lines)

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.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.
  • D Offline
    D Offline
    deleted343
    wrote on 7 Sept 2021, 10:02 last edited by deleted343 9 Jul 2021, 10:05
    #1

    Hello,

    I have already posted this question here but it got no answers at all : https://stackoverflow.com/questions/69042445/adjust-qtableview-height-to-its-content-few-lines

    Below a screenshot of my application. I want to get rid of the white spaces after the last tables lines marked by red rectangles :

    Screenshot

    The horizontal size policy is expanding and the vertical one is minimum and for other tables it is both set to expanding.

    I'm using this method I found in another SO question but as you can see the result is not flawless.

    void verticalResizeTableViewToContents(QTableView* tableView)
        {
            tableView->resizeRowsToContents();
    
            // does it work ?
            tableView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    
            int rowTotalHeight = 0;
    
            // Rows height
            int count = tableView->verticalHeader()->count();
            for (int i = 0; i < count; ++i) {
                // 2018-03 edit: only account for row if it is visible
                if (!tableView->verticalHeader()->isSectionHidden(i)) {
                    rowTotalHeight += tableView->verticalHeader()->sectionSize(i);
                }
            }
    
            // Check for scrollbar visibility
            if (!tableView->horizontalScrollBar()->isHidden())
            {
                rowTotalHeight += tableView->horizontalScrollBar()->height();
            }
    
            // Check for header visibility
            if (!tableView->horizontalHeader()->isHidden())
            {
                rowTotalHeight += tableView->horizontalHeader()->height();
            }
            tableView->setMaximumHeight(rowTotalHeight);
        }
    

    Somewhere, I'm using this code to setup one of the tables :

    m_Internals->Ui.Measures->setModel(mm->getPh66MeasuresModel());
        m_Internals->Ui.Measures->horizontalHeader()->setSectionsMovable(true);
        m_Internals->Ui.Measures->horizontalHeader()->setHighlightSections(false);
        m_Internals->Ui.Measures->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
        m_Internals->Ui.Measures->horizontalHeader()->setStretchLastSection(true);
    
    m_Internals->Ui.Measures->verticalHeader()->hide();
        m_Internals->Ui.Measures->setSelectionBehavior(QAbstractItemView::SelectRows);
        verticalResizeTableViewToContents(m_Internals->Ui.Measures);
    

    I'm using Qt ModelView pattern to populate/update the tables.

    Thanks.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dan203
      wrote on 7 Sept 2021, 19:41 last edited by
      #2

      Are the number of rows fixed or dynamic?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted343
        wrote on 8 Sept 2021, 10:24 last edited by
        #3

        The number of rows are fixed !

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 8 Sept 2021, 10:30 last edited by
          #4

          You don't have to do it manually, just call view->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

          D 1 Reply Last reply 8 Sept 2021, 10:35
          1
          • V VRonin
            8 Sept 2021, 10:30

            You don't have to do it manually, just call view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);

            D Offline
            D Offline
            deleted343
            wrote on 8 Sept 2021, 10:35 last edited by deleted343 9 Aug 2021, 10:41
            #5

            @VRonin I removed verticalResizeTableViewToContents() and used your code in the constructor and I still have the same resultats as verticalResizeTableViewToContents(). I have also set it in the UI file.

            When I shrink the windows and make scroll bars appears on the table view, there is no extra white space but I think it's normal.

            and if I don't do nothing at all the extra white space is larger !

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deleted343
              wrote on 8 Sept 2021, 10:46 last edited by deleted343 9 Aug 2021, 10:54
              #6

              and even in I set the number of rows in the model to 0, I still have a white space after the columns header :

              int MeasuresModel::rowCount(const QModelIndex& parent /*= QModelIndex()*/) const
              {
              	Q_UNUSED(parent)
              	
                  return 0;
              }
              

              I noticed something very weired when I use this size policy :

              m_Internals->Ui.Measures->setSizePolicy(QSizePolicy::Policy::Minimum, QSizePolicy::Policy::Minimum);
              

              The width of the windows keeps growing indefinitely !!! Anyway, I even tried changing the size policy of the parents.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dan203
                wrote on 8 Sept 2021, 19:24 last edited by
                #7

                Do you have a spacer at the bottom to fill the empty space? You need something to fill the empty space or you'll get weird results from the layout widget.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  embeddedmz_2
                  wrote on 10 Sept 2021, 07:09 last edited by
                  #8

                  I have already a spacer, I also tried by adding another one but the table is still taking space.

                  Btw, when I deleted my Qt account because I was unable to download the open source installer with it, it deleted also my forum account :(

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    embeddedmz_2
                    wrote on 10 Sept 2021, 12:21 last edited by embeddedmz_2 9 Oct 2021, 16:55
                    #9

                    Update : I made a small example to reproduce this issue with QTableView : https://github.com/embeddedmz/QTableViewAdjustPolicyNotWorkingProperly

                    Using the latest Qt version (from Qt official installer), there's no issue. However, using the Qt library provided by vcpkg (outdated for sure) the issue is there.

                    With Qt provided by vcpkg :
                    alt text

                    With the latest Qt provided by the Qt Company :
                    alt text

                    I will make a git pull on my vcpkg repo, update Qt and check if this issue disappears with the last version of Qt provided by vcpkg ! With vcpkg last Qt5 version the issue is still present and Qt6 libraries have an issue that I reported here : https://github.com/microsoft/vcpkg/issues/20101

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      embeddedmz_2
                      wrote on 13 Sept 2021, 07:31 last edited by
                      #10

                      I have tested the code I published on github on Windows 8.1 + vcpkg and I don't have the issue that I got with Win10 + vcpkg.

                      On StackOverflow.com, someone gave me this workaround :

                      you can also fix your problem with one trick, this problem happens for you because your data is lower than the table size. I clone your project and change sizepolicy
                      

                      alt text

                      mrjjM 1 Reply Last reply 13 Sept 2021, 08:32
                      0
                      • E embeddedmz_2
                        13 Sept 2021, 07:31

                        I have tested the code I published on github on Windows 8.1 + vcpkg and I don't have the issue that I got with Win10 + vcpkg.

                        On StackOverflow.com, someone gave me this workaround :

                        you can also fix your problem with one trick, this problem happens for you because your data is lower than the table size. I clone your project and change sizepolicy
                        

                        alt text

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 13 Sept 2021, 08:32 last edited by
                        #11

                        @embeddedmz_2

                        Hi
                        So did the work around fix it ?

                        E 1 Reply Last reply 24 Aug 2022, 13:37
                        0
                        • mrjjM mrjj
                          13 Sept 2021, 08:32

                          @embeddedmz_2

                          Hi
                          So did the work around fix it ?

                          E Offline
                          E Offline
                          embeddedmz_2
                          wrote on 24 Aug 2022, 13:37 last edited by
                          #12

                          @mrjj the issue is with QSize QAbstractScrollArea::sizeHint() which is not well implemented (buggy), I have already reported this issue in the report QTBUG-101874.

                          After reading the code of sizeHint, I found that by setting the verticalScrollBarPolicy to ScrollBarAlwaysOff, the extra space will not be added at the end of the table.

                          Under Qt 5.12.11, there's no issue since the sizeHint's implementation is different from that of more recent versions of Qt.

                          Hope that helps other people.

                          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