QTableWidget automatically sizes rows too large
-
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? -
@JoeCFD
OK what is a section in this context?Also, why would a call to table->setRowHeight(row, height) have no effect?
@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.
-
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?@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?!
-
Do you have custom views/delegates?
The height of each row is controlled by QAbstractItemView::sizeHintForRow
-
@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?!
@JonB Using the horizontal header causes the column widths to be affected. Nothing ever wraps.
-
@JonB Using the horizontal header causes the column widths to be affected. Nothing ever wraps.
@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.
-
@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.
@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. -
@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.@Publicnamer
do this in vertical header
void setMaximumSectionSize(int size)
void setMinimumSectionSize(int size) -
@Publicnamer
do this in vertical header
void setMaximumSectionSize(int size)
void setMinimumSectionSize(int size)@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.
-
@Publicnamer
do this in vertical header
void setMaximumSectionSize(int size)
void setMinimumSectionSize(int size)@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.
-
@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.
@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?
-
@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?
@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.
-
@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.
@Publicnamer You can basically set the same font to each item. Then you will will not have any problem.
-
@Publicnamer You can basically set the same font to each item. Then you will will not have any problem.
@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.
-
@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 ); -
@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 );@JoeCFD OK but each of my rows can be a different height, so that doesn't help me.
-
@JoeCFD OK but each of my rows can be a different height, so that doesn't help me.
@Publicnamer
try this
m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size) -
@Publicnamer
try this
m_tableWidget->verticalHeader()-> resizeSection(int logicalIndex, int size)@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.
-
@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.
@Publicnamer
horizontal header section defines column width.
vertical header section defines row height. -
@Publicnamer
horizontal header section defines column width.
vertical header section defines row height.@JoeCFD
OK what is a section in this context?Also, why would a call to table->setRowHeight(row, height) have no effect?
-
@JoeCFD
OK what is a section in this context?Also, why would a call to table->setRowHeight(row, height) have no effect?
@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.