PDA

View Full Version : Issues with resizing QTableView nested in another widget



forgottenduck
27th March 2015, 16:07
I'm having a few issues with sizing my QTableView subclass which is nested inside a few other widgets. What I'm trying to achieve is a TableView that increases it's vertical size to accommodate all the rows in the table. I've been able to get close to this, but not quite there. I have a suspicion that I am approaching some aspects of the problem the wrong way.

The basic layout of my Application is this:

MainWindow contains a TabWidget the Tabs have a ScrollArea which contains several collapsible panels in a vertical layout that serve as containers for various input widgets. Some of those panels contain my TableView class. In order to get the correct sizing for my TableVIew, I implemented SizeHint, and defined it as an override because otherwise it wouldn't be called at all. I'm fairly certain this is because Tables typically use SizeHintForIndex, SizeHintForColumn, and SizeHintForRow, but I really only need to base my sizing on the table itself, so hopefully this is an acceptable solution.


QSize PTableView::sizeHint() const{
QSize size = QTableView::sizeHint();
size.setHeight(defaultCellHeight * model()->rowCount() + 45);
return size;
}

The good news is this works perfectly when the widget is constructed. SizeHint is called appropriately, and the TableView is the correct vertical size (horizontal size isn't an issue as it simply takes up all available space). So my issue is that when the user adds a new row, or deletes a row, the size does not update. So I tried to make this connection to solve the problem:



connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(recalculateSize()));
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(recalculateSize()));

void PTableView::recalculateSize(){
adjustSize();
}


This produced undesirable results. The vertial size is correct for the TableView, but the container widget does not resize to match. Additionally, when the window is resized the widget reverts back to its constructed size.

So my question I suppose boils down to: how can I tell my widget to resize in the same way that it does when the tab is constructed? If I add a bunch of rows, close the project, then reopen it, the Table will have been resized to the correct size, so that is the sizing I need.

If need be, I can post my full classes or a compilable example, but I think I just need a push in the right direction to figure it out.

anda_skoa
27th March 2015, 18:00
You might have to call activate() on the view's parent's layout.

Cheers,
_

forgottenduck
27th March 2015, 21:08
You might have to call activate() on the view's parent's layout.

Cheers,
_

I tried calling activate at various levels of my widget hierarchy, and none seemed to produce any noticeable effect. The oddest thing to me is that the size is calculated correctly when the widgets within the tab are all constructed. I just can't seem to determine how I can make that happen again. I tried looking at the call stack when sizeHint is called during construction, but I didn't really get much info from that except that the GridLayout that the view is in seems to be asking for it's size.

Added after 6 minutes:

Actually I might have a workable solution now. Instead of calling adjust size, I am simply setting a fixed height based on my sizeHint. It's not the best solution, but I don't think it will cause any issues. I do wish I understood the sizing and layout system better though.