PDA

View Full Version : main window resize question



schnitzel
22nd May 2010, 22:15
I would like to dynamically add/remove labels in a gridlayout at run time. Adding them is not a problem and the main window resizes appropriately. When I then remove those labels again, the Main window doesn't shrink back, although I can manually resize the main window back to its original size.
What methods am I missing?

I have attached a sample project.

schnitzel
25th May 2010, 20:15
I am still not sure on how to solve this issue.
Hopefully the following code will help to locate the missing calls in order to resize the main window. Here is how I add the new widgets at runtime:


lbltest = new QLabel(ui->tabw);
lbltest->setObjectName(QString::fromUtf8("lbltest"));

lbltest->setAlignment(Qt::AlignCenter);
lbltest->setText("This is a test");

ui->gl1->addWidget(lbltest,4,0,1,1);

lbltest2 = new QLabel(ui->tabw);
lbltest2->setObjectName(QString::fromUtf8("lbltest2"));

lbltest2->setAlignment(Qt::AlignCenter);
lbltest2->setText("test");

ui->gl1->addWidget(lbltest2,5,0,1,1);


This will work as expected and the mainwindow grows to accomodate the extra widgets.

Now I try to remove the labels with the following code:



if (lbltest != NULL)
{
ui->gl1->removeWidget(lbltest);
delete lbltest;
lbltest = NULL;
}
if (lbltest2 != NULL)
{
ui->gl1->removeWidget(lbltest2);
delete lbltest2;
lbltest2 = NULL;
}


I was hoping the mainwindow would shrink back to its original size, but instead, the original widgets expand into the additional space. At this point I can manually resize by dragging the border of the mainwindow back to the original size. Is it possible to achieve this programmatically?

jryannel
25th May 2010, 20:31
Haven't tried it but maybe QWidget::updateGeometry() will help. See http://doc.qt.nokia.com/latest/qwidget.html#updateGeometry

schnitzel
25th May 2010, 20:53
Haven't tried it but maybe QWidget::updateGeometry() will help. See http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#updateGeometry

I tried to add

this->updateGeometry();

just after removing the widgets, but it didn't help.

jryannel
26th May 2010, 08:01
Here is how the dialogs make an expansion: Extension Example. Not sure if this helps.

In general the window doesn't know it's previous size, its calculated based on the sum of the sizeHints() from all widgets inside the layouts. Some widgets in your UI are happy with greater sizes. So another option would be to set explicit QSizePolicy on them to tell them sizeHint() is minimum and good, e.g. QSizePolicy::Minimum.

Take care!

wysota
26th May 2010, 08:40
Try setting the size constraint on the top widget's layout to SetFixedSize. The widget will then grow or shrink depending on what its layout needs.

schnitzel
26th May 2010, 17:07
Try setting the size constraint on the top widget's layout to SetFixedSize. The widget will then grow or shrink depending on what its layout needs.

By top widget, I assume you mean the MainWindow. I tried it but it didn't work. A test project is attached to the first post of this thread.

Here is the current layout:

4688

wysota
26th May 2010, 20:03
Not the size policy but size constraint of the layout. Scroll down until you see properties of the layout. You may also have to do the same for the layout of the central widget.

schnitzel
26th May 2010, 20:38
Not the size policy but size constraint of the layout. Scroll down until you see properties of the layout. You may also have to do the same for the layout of the central widget.

I tried changing the layout size constraints. Since there are three layouts in my test form, I tried different combinations setting layoutsizeconstraint to SetFixedSize and SetMinimumSize, but it still doesn't work. Are you able to make it work in the test project? I must be doing something else wrong.

wysota
27th May 2010, 00:04
You probably have to set the size constraint for the layout of the main window. You can't do it from within Designer, you have to code it manually in your project.


#include <QtGui>

class Widget : public QWidget {
public:
Widget() : QWidget() {
l = new QVBoxLayout(this);
for(int i=0;i<3;++i)
l->addWidget(new QPushButton);
x = 0;
startTimer(2000);
}
protected:
void timerEvent(QTimerEvent *e){
l->addWidget(new QPushButton);
if(++x==7) killTimer(e->timerId());
}
private:
QVBoxLayout *l;
int x;
};

int main(int argc, char **argv){
QApplication app(argc, argv);
QMainWindow mw;
mw.layout()->setSizeConstraint(QLayout::SetFixedSize);
Widget *w = new Widget;
mw.setCentralWidget(w);
mw.show();
return app.exec();
}

schnitzel
27th May 2010, 18:29
You probably have to set the size constraint for the layout of the main window. You can't do it from within Designer, you have to code it manually in your project.


Hi wysota,
I tried your suggestion and it works somewhat in the test app - 'somewhat' because now I can't resize it any more. My actual problem is more like this (http://www.qtcentre.org/threads/27445-Resize-main-window-after-hiding-an-element)post. Hiding vs. removing the widgets from the layout can be thought of as equivalent - or so I thought.
With my test app I was trying to mimic what is going on in my actual application.

Sometimes it is difficult to describe subtle visual behavior in GUI applications.

I designed my application's GUI in Qt Creator's designer and numerous widgets that I have placed are being made invisible at run time - yet the space they have initially occupied is still shown but of course is empty. That is the problem I was trying to solve. The entire thing is not a deal breaker as the main form can simple be made smaller by resizing it manually by clicking and dragging the border after the application starts. I was just hoping for a nice programmatical solution.

Thanks for looking into this and I'm sorry if I wasted your time.

wysota
27th May 2010, 20:03
I tried your suggestion and it works somewhat in the test app - 'somewhat' because now I can't resize it any more. My actual problem is more like this (http://www.qtcentre.org/threads/27445-Resize-main-window-after-hiding-an-element)post. Hiding vs. removing the widgets from the layout can be thought of as equivalent - or so I thought.
There is no sane way of adjusting the window size automatically and keeping the ability of resizing the window. Think what should happen if you resize the window manually and then an element gets hidden - should the window adjust its size (because an element was hidden) or retain it (because you resized it manually)? You can't eat the cake and keep the cake. You can program it manually but whichever behaviour you choose the other one will not work. You can try different size constraints but none of them will do exactly what you expect to achieve.

neeraj.talreja
29th May 2010, 09:31
There is no sane way of adjusting the window size automatically and keeping the ability of resizing the window. Think what should happen if you resize the window manually and then an element gets hidden - should the window adjust its size (because an element was hidden) or retain it (because you resized it manually)? You can't eat the cake and keep the cake. You can program it manually but whichever behaviour you choose the other one will not work. You can try different size constraints but none of them will do exactly what you expect to achieve.

You should use adjustResize() function in your application which set the size hint of application.
i have done the same way.it is working