QTableWidget cells are QListWidgets resizeRowToContents
I have a QTableWidget inside a QVBoxLayout. Its cells are comprised of QListWidgets.
How can I resize the rows of the table to take in account the number of QListWidgetItems.
The table header and cells expand and contract fine, but when I go to full screen, I'd like the row to expand to show the maximum number of list items for that row.
Also, if a row has fewer items, the row should be shorter.
Code:
listwidget
->setResizeMode
(QListView::Adjust);
// has no effecttableWidget->resizeRowToContents(row); // has no effect
Re: QTableWidget cells are QListWidgets resizeRowToContents
Try "tableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );"
I also use "horizontalHeader()->setStretchLastSection( true );" but maybe not needed here.
Maybe you need to reimplement sizeHint / minimumSizeHint too.
Re: QTableWidget cells are QListWidgets resizeRowToContents
QHeaderView::ResizeToContents breaks the expanding layout, i.e. the cells no longer expand/contract when resizing the window.
The following works as far as resizing goes, but all the cells are the same size. If a row has only 2 lines max (2 listWidgetItems), I want that entire row to resizeRowToContents() and display 2 lines.
Code:
QHeaderView* columnheader
= ui
->tableWidget
->horizontalHeader
();
columnheader
->setSectionResizeMode
(QHeaderView::Stretch);
QHeaderView* rowheader
= ui
->tableWidget
->verticalHeader
();
Re: QTableWidget cells are QListWidgets resizeRowToContents
You don't mention what you are using as a model, but I would suggest that you do this.
Derive from QAbstractItemModel. Implement Qt::SizeHintRole case for QAbstractItemModel::data(). In this, compute the dimensions of each list based on the number of items in it, font, etc. and return that as a QSize. I believe the table view will use this to set the row and column dimensions by choosing the maximum for each respective row and column.
It sounds like your model is dedicated to viewing in a single table view. If you want the cell sizes to change based on whether the table view is full screen or not, then setting a flag in the model to indicate the view state would let you switch between a compressed or fully-expanded list size in the data() method.
I also presume you are using QAbstractItemView::setIndexWidget() when you create the list widgets for each cell. You might also need to store this pointer in the model using QAbstractItemModel::setData() (maybe under Qt::UserRole) so you can retrieve it when calculating the size hint.
Edit: I think what is happening now is that the cells might be sized according to the list widgets' minimumSizeHint(), so trying to change things by manipulating table properties isn't going to work. You might be able to verify this by setting a minimumSizeHint() for each list widget and see if the layout changes.
Re: QTableWidget cells are QListWidgets resizeRowToContents
Quote:
You might be able to verify this by setting a minimumSizeHint() for each list widget and see if the layout changes.
I can't find anything like setMinimumSizeHint().
Re: QTableWidget cells are QListWidgets resizeRowToContents
My bad. If you implement a custom widget, you can override minimumSizeHint() to return something other than the default (which for QWidget is an invalid size). Try setting a minimumSize instead - something much larger than the size your table cells are now. A minimumSize that is larger than the default will take precedence over the minimumSizeHint.
Re: QTableWidget cells are QListWidgets resizeRowToContents
Setting a larger minimumSize just hoses the table grid, perhaps the table row size, yet the listwidgets remain the same size.
The one thing that affects the row size is numbers of rows.
If I break the centralWidget vertical layout, things like
Code:
listWidget->setMaximumHeight(100);
tableWidget->setCellWidget(1, 1, listWidget);
tableWidget->resizeRowToContents(1);
work great.
The culprit is the layout.
Re: QTableWidget cells are QListWidgets resizeRowToContents
Quote:
The one thing that affects the row size is numbers of rows.
You must be doing something funky. I don't recall ever seeing a table view's row size change when the number of rows changes. If there are fewer rows than the space available, then the space below the bottom row is just empty. The rows don't expand to fit the size.