Adjust QTableView Height to its content (few lines)
-
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 :
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.
-
The number of rows are fixed !
-
You don't have to do it manually, just call
view->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
@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 !
-
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.
-
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 :(
-
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 :
With the latest Qt provided by the Qt Company :
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
-
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
-
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
Hi
So did the work around fix it ? -
Hi
So did the work around fix it ?@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.