Results 1 to 13 of 13

Thread: main window resize question

  1. #1
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Attached Files Attached Files

  2. The following user says thank you to schnitzel for this useful post:

    Ishmael (3rd June 2010)

  3. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:
    Qt Code:
    1. lbltest = new QLabel(ui->tabw);
    2. lbltest->setObjectName(QString::fromUtf8("lbltest"));
    3.  
    4. lbltest->setAlignment(Qt::AlignCenter);
    5. lbltest->setText("This is a test");
    6.  
    7. ui->gl1->addWidget(lbltest,4,0,1,1);
    8.  
    9. lbltest2 = new QLabel(ui->tabw);
    10. lbltest2->setObjectName(QString::fromUtf8("lbltest2"));
    11.  
    12. lbltest2->setAlignment(Qt::AlignCenter);
    13. lbltest2->setText("test");
    14.  
    15. ui->gl1->addWidget(lbltest2,5,0,1,1);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. if (lbltest != NULL)
    2. {
    3. ui->gl1->removeWidget(lbltest);
    4. delete lbltest;
    5. lbltest = NULL;
    6. }
    7. if (lbltest2 != NULL)
    8. {
    9. ui->gl1->removeWidget(lbltest2);
    10. delete lbltest2;
    11. lbltest2 = NULL;
    12. }
    To copy to clipboard, switch view to plain text mode 

    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?

  4. #3
    Join Date
    Sep 2008
    Location
    Munich
    Posts
    32
    Thanked 8 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main window resize question

    Last edited by wysota; 26th May 2010 at 09:39.

  5. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main window resize question

    Quote Originally Posted by jryannel View Post
    I tried to add
    Qt Code:
    1. this->updateGeometry();
    To copy to clipboard, switch view to plain text mode 

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

  6. #5
    Join Date
    Sep 2008
    Location
    Munich
    Posts
    32
    Thanked 8 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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!
    Last edited by wysota; 26th May 2010 at 09:41. Reason: reformatted to look better

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main window resize question

    Quote Originally Posted by wysota View Post
    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:

    resize2..JPG
    Last edited by schnitzel; 26th May 2010 at 18:08. Reason: fix layout

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main window resize question

    Quote Originally Posted by wysota View Post
    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.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. public:
    5. Widget() : QWidget() {
    6. l = new QVBoxLayout(this);
    7. for(int i=0;i<3;++i)
    8. l->addWidget(new QPushButton);
    9. x = 0;
    10. startTimer(2000);
    11. }
    12. protected:
    13. void timerEvent(QTimerEvent *e){
    14. l->addWidget(new QPushButton);
    15. if(++x==7) killTimer(e->timerId());
    16. }
    17. private:
    18. int x;
    19. };
    20.  
    21. int main(int argc, char **argv){
    22. QApplication app(argc, argv);
    23. mw.layout()->setSizeConstraint(QLayout::SetFixedSize);
    24. Widget *w = new Widget;
    25. mw.setCentralWidget(w);
    26. mw.show();
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    mnunberg (19th September 2010)

  13. #11
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: main window resize question

    Quote Originally Posted by wysota View Post
    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.

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: main window resize question

    Quote Originally Posted by schnitzel View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following 2 users say thank you to wysota for this useful post:

    Ishmael (3rd June 2010), schnitzel (27th May 2010)

  16. #13
    Join Date
    May 2010
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: main window resize question

    Quote Originally Posted by wysota View Post
    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

Similar Threads

  1. Resize main window after hiding an element
    By Swankee in forum Newbie
    Replies: 2
    Last Post: 23rd February 2012, 00:23
  2. Replies: 9
    Last Post: 16th May 2010, 17:21
  3. Replies: 11
    Last Post: 11th August 2008, 10:14
  4. Replies: 15
    Last Post: 23rd March 2007, 17:16
  5. Main window
    By Michiel in forum Qt Tools
    Replies: 1
    Last Post: 21st March 2006, 00:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.