QTableWidget white space - why and how?
-
wrote on 24 Apr 2016, 16:10 last edited by
Hello,
Using a
QTableWidget
withsetSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
adjusts the size of the table to fit contents. However, sometimes, there is white space left to the right or below the table.Why does this white space appear? And how can I get rid of it WITHOUT simply stretching the last section, because in tables with only 1 row you get a funny looking row that's too stretched.
Is there some built-in way of resizing the table itself to have no extra space?
-
Hi,
Can you post an image showing that white space ?
-
wrote on 28 Apr 2016, 23:34 last edited by Lobstw
-
wrote on 29 Apr 2016, 02:35 last edited by
Your first picture displays white space below the row because
QTableView height > row+header height.
The are 2 ways to avoid it - adjust header or row height
or adjust height of the table ( you may need to set minimumSize first )
setMinimumSize(QSize(0, 3)); // overrides minimum height of QTableView to 3 pixelsIt looks like second picture shows space where vertical scrollbar should be shown.
I am not sure why it is not shown, If you deliberately hidden it should have not take any space.
check what is set withQt::ScrollBarPolicy verticalScrollBarPolicy () const
void setVerticalScrollBarPolicy ( Qt::ScrollBarPolicy ) -
Your first picture displays white space below the row because
QTableView height > row+header height.
The are 2 ways to avoid it - adjust header or row height
or adjust height of the table ( you may need to set minimumSize first )
setMinimumSize(QSize(0, 3)); // overrides minimum height of QTableView to 3 pixelsIt looks like second picture shows space where vertical scrollbar should be shown.
I am not sure why it is not shown, If you deliberately hidden it should have not take any space.
check what is set withQt::ScrollBarPolicy verticalScrollBarPolicy () const
void setVerticalScrollBarPolicy ( Qt::ScrollBarPolicy )wrote on 29 Apr 2016, 20:48 last edited by@alex_malyu My table is part of a GridLayout so resizing/setting minimum size didn't fix it. (I'd prefer not changing row size)
If I set ScrollBarPolicy to AlwaysOff, then the space on the right is gone. But I see that the default policy is "as needed" and on Mac that usually means the transparent scroll bar appears over the content not with a seperate space for it. Is there some other setting for it in Qt or any way of recreating this interaction?
-
@alex_malyu My table is part of a GridLayout so resizing/setting minimum size didn't fix it. (I'd prefer not changing row size)
If I set ScrollBarPolicy to AlwaysOff, then the space on the right is gone. But I see that the default policy is "as needed" and on Mac that usually means the transparent scroll bar appears over the content not with a seperate space for it. Is there some other setting for it in Qt or any way of recreating this interaction?
wrote on 29 Apr 2016, 21:39 last edited bysetting minimum size would only allow it to be smaller ( you might want to compute desired height yourself.) If you are always working with a single row you can make it of fixed height / or set maximum size and spacer, but if you put it into layout where a few widget define the height, you need something to be resized instead of table view. You for example can put table view and vertical spacer into vertical layout in your grid layout instead of table view, but in the most cases this would result in even less attractive look.
as for ScrollBarPolicy on Windows and Linux with Qt4 (I do not have access to Qt5 right now to test, but as far as I can recall it should be the same) scrollbar area takes away some space from available for cells space, but this only happens if scrollbar is visible (unless it is always on ).
In your case behaviour corresponds to always on. Sounds like a bug. -
setting minimum size would only allow it to be smaller ( you might want to compute desired height yourself.) If you are always working with a single row you can make it of fixed height / or set maximum size and spacer, but if you put it into layout where a few widget define the height, you need something to be resized instead of table view. You for example can put table view and vertical spacer into vertical layout in your grid layout instead of table view, but in the most cases this would result in even less attractive look.
as for ScrollBarPolicy on Windows and Linux with Qt4 (I do not have access to Qt5 right now to test, but as far as I can recall it should be the same) scrollbar area takes away some space from available for cells space, but this only happens if scrollbar is visible (unless it is always on ).
In your case behaviour corresponds to always on. Sounds like a bug.wrote on 1 May 2016, 00:06 last edited by@alex_malyu Tried adding a spacer between my widgets and the space still remains :(
I also think the scrollbar one is a bug because the same code works without ANY spaces on Windows and it's only Mac giving me grief
-
wrote on 1 Sept 2017, 15:11 last edited by
Has anyone found a solution to this? I'm using a QTableWidget on macOS and having the same issue. The table is always at least 76 pixels tall, even when the verticalHeader is smaller than that. Changing the minimum height for the table and/or the header (to something small like 3 pixels) doesn't change the table height; it's still 76. Once I add more rows (so that the height is > 76) the table works just fine.