PDA

View Full Version : scrollArea not updating



user
28th September 2007, 01:08
I hope someone can help me with my scrollArea problem:

I defined a scroll area and set a tab widget in it:

scrollArea_ = new QScrollArea;
setCentralWidget( scrollArea_);
scrollArea_->setWidgetResizable ( true );
scrollArea_->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
scrollArea_->setVerticalScrollBarPolicy( Qt::ScrollBarAsNeeded
scrollAreaGridLayout_ = new QGridLayout(scrollArea_);
scrollAreaWidget_ = new QWidget();
scrollAreaGridLayout_->addWidget(scrollAreaWidget_, 0, 1, 1, 1);
scrollArea_->setWidget(scrollAreaWidget_);
tabWidget_ = new QTabWidget();
scrollAreaGridLayout2_ = new QGridLayout(scrollAreaWidget_);
scrollAreaGridLayout2_->addWidget(tabWidget_, 0, 1, 1, 1);
tabWidget_->setEnabled(true);

So far everything is fine. Later in the code I change the tabWidget_ contents (and size) and the scrollArea displays correctly.
Depending on the user actions this scrollArea_ should display different things.
So when I change the display:

tabWidget_->hide();
newProfile_->show(); // Another QTabWidget()
scrollAreaGridLayout2_->addWidget(newProfile_, 0, 1, 1, 1);

The scrollArea gets updated with the new size and appropriate scroll bars.

Problem: my program changes the newProfile_ QabWidget to display other tabs which are bigger but it seems the scrollArea and the scroll bars are not updated and I can't scroll to see the bigger tabs. I tried to get the new size of the tab and resize the QabWidget and the scrollArea but nothing seems to help.

newProfile_->hide();
newProfile_->resize( newQWidgetWidth(), newQWidgetHeight() );
newProfile_->updateGeometry();
scrollArea_->resize( newQWidgetWidth(), newQWidgetHeight() );
scrollArea_->updateGeometry();
scrollAreaWidget_->resize( newQWidgetWidth(), newQWidgetHeight() );
scrollAreaWidget_->updateGeometry();
missionProfile_->show();
scrollAreaGridLayout2_->addWidget(newProfile_, 0, 1, 1, 1);

Can anyone suggest a solution?

wysota
29th September 2007, 10:22
Did you apply layouts everywhere? Based on the fact that you use resize() and updateGeometry() I think not. And you should have.

user
1st October 2007, 00:07
I do have layouts everywhere.
I found a solution to my problem. I don't know if it's the best way, so if there's a better way please let me know. Also an explanation why it works would be helpful.

My solution was to add another line:

newProfile_->resize( newQWidgetWidth(), newQWidgetHeight() );
newProfile_->setMinimumSize( newQWidgetWidth(), newQWidgetHeight() );
newProfile_->updateGeometry();
and make sure all the tabs I want to display have their minimum size defined.

wysota
1st October 2007, 05:17
If you use resize() and updateGeometry then you are not using layouts everywhere...

user
2nd October 2007, 00:11
Ok, I removed resize() and updateGeometry() and only left setMinimumSize() and it still works. So I guess I only need this one command.

wysota
2nd October 2007, 00:16
What kind of widget is "newprofile_"? Is it a custom widget, a basic Qt widget or a complex widget composed of other widgets?

user
2nd October 2007, 00:48
newprofile_ is a class that inherits Qwidget. It contains another QWidget X which has buttons and frames. The frames will eventually display a Qwt plot. Its parent is a QMainWindow.
Depending on user choice, I show/hide X. When X is hidden, another Qwidget Y is shown in its place. Y is also a class that inherits QWidget and I use the same parent (QMainWindow). An instance of Y is created inside newprofile_.
So the display can change between X and Y but both are on newprofile_ widget.

Actually, while trying to explain all that, I realised that maybe the parent of Y should be newprofile_ and not QMainWindow. I changed my code and now the layout works without setMinimumSize() and without me having to do anything special.
Thanks wysota. Sometimes it helps just trying to explain the things I do.

wysota
2nd October 2007, 11:42
A side note - for switching between X and Y you could use QStackedWidget. It would improve your design a little (as I assume you are creating/showing/hiding those widgets yourself).

I'm digging the subject, because you were certainly missing a layout or a size hint here somewhere, but it's hard to say where without knowing the details.

user
3rd October 2007, 01:21
I only create the widgets once (I have 13 of them) and let the user select from a QTreeWidget which one to show. I need to do it this way since each type of widget can appear multiple times but present different info depending on it's location in the tree and user input.

I had a look at QStackedWidget. It displays arrows (to move between the widgets), which probably was the reason I chose not to use it and I'm not sure if the size of the scroll area will adjust for different widget sizes if I put them all in the stacked widget or just always show the biggest size.
Other than that it does seem like a good idea. I think the way I wrote the code is very similar to QStackedWidget just using show/hide instead of signals. I have to finish other parts of the application first but I will make a note to come back to this one and see if it makes a difference.

I think my original problem stemmed from not having the right parent to all those new widgets. Once that was fixed everything worked well and I didn't have to set anything more.

wysota
3rd October 2007, 09:17
I had a look at QStackedWidget. It displays arrows (to move between the widgets), which probably was the reason I chose not to use it and I'm not sure if the size of the scroll area will adjust for different widget sizes if I put them all in the stacked widget or just always show the biggest size.
The arrows are only in Designer. They are not there in a real application.