1 Attachment(s)
main window resize question
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.
Re: main window resize question
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:
Code:
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:
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?
Re: main window resize question
Re: main window resize question
Quote:
Originally Posted by
jryannel
I tried to add
Code:
this->updateGeometry();
just after removing the widgets, but it didn't help.
Re: main window resize question
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!
Re: main window resize question
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.
1 Attachment(s)
Re: main window resize question
Quote:
Originally Posted by
wysota
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:
Attachment 4688
Re: main window resize question
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.
Re: main window resize question
Quote:
Originally Posted by
wysota
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.
Re: main window resize question
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.
Code:
#include <QtGui>
public:
for(int i=0;i<3;++i)
x = 0;
startTimer(2000);
}
protected:
if(++x==7) killTimer(e->timerId());
}
private:
int x;
};
int main(int argc, char **argv){
mw.
layout()->setSizeConstraint
(QLayout::SetFixedSize);
Widget *w = new Widget;
mw.setCentralWidget(w);
mw.show();
return app.exec();
}
Re: main window resize question
Quote:
Originally Posted by
wysota
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 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.
Re: main window resize question
Quote:
Originally Posted by
schnitzel
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 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.
Re: main window resize question
Quote:
Originally Posted by
wysota
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