Results 1 to 6 of 6

Thread: Toggling the size of a window

  1. #1
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Toggling the size of a window

    Hi Guys

    I have a window that contains 2 widgets in a vertical splitter and opens with only the top widget showing with no space below it.
    I want to be able to:
    1. Click a button and have the bottom widget show which causes the window to increase in height by extending downwards, to accommodate the second widget. (I can do this.)
    2. When the button is clicked a second time, I want the second widget to hide again and the window to resize to its original size - no problem getting it to hide, but the window won't resize.

    Any help appreciated.

    Jeff

  2. #2
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggling the size of a window

    Quote Originally Posted by Jeffb View Post
    Hi Guys

    I have a window that contains 2 widgets in a vertical splitter and opens with only the top widget showing with no space below it.
    I want to be able to:
    1. Click a button and have the bottom widget show which causes the window to increase in height by extending downwards, to accommodate the second widget. (I can do this.)
    2. When the button is clicked a second time, I want the second widget to hide again and the window to resize to its original size - no problem getting it to hide, but the window won't resize.

    Any help appreciated.

    Jeff
    Hi Jeff,

    Since you can show/hide the bottom one by catching the button click event, you must be able to resize the main window as well. It should be very easy and simple. All you need is to have the pointer of the base window and resizing window widget by using resize() method.

    Regards,
    Dong Back Kim

  3. #3
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Toggling the size of a window

    @Dong
    Yep - I've tried that. I got the size of the main window before showing the bottom widget. And then after hiding the bottom widget call resize on the main window. It simply does not resize.
    I have no idea why.

    Jeff

  4. #4
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggling the size of a window

    Quote Originally Posted by Jeffb View Post
    @Dong
    Yep - I've tried that. I got the size of the main window before showing the bottom widget. And then after hiding the bottom widget call resize on the main window. It simply does not resize.
    I have no idea why.

    Jeff
    Hi Jeff,

    hmmm that's bit weird because I just tried it out myself and it works perfectly.

    Qt Code:
    1. void MainWindow::on_pushButtonTest_clicked()
    2. {
    3. this->resize(this->width() + 10, this->height() + 10);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I could see the window size is increasing every single time I click the button.

    Regards,
    Dong Back Kim

  5. #5
    Join Date
    Apr 2010
    Posts
    77
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X

    Smile Re: Toggling the size of a window

    @Dong

    Finally got it solved. Your last reply got me to set up a simple MainWindow to experiment.

    Here's the code for anyone who's interested.
    Any improvements are welcome.
    Remember that this shows and hides a bottom widget where the top and bottom widgets are inside a splitter.
    Note: splitterSizes is a QList<int>

    In constructor:
    Qt Code:
    1. // hide the bottom widget
    2. int topWidgetHeight = ui->topWidget->sizeHint().height(); // top widget height
    3. bottomWidgetHeight = 100 + ui->splitter->handleWidth(); // I set the bottomWidgetHeight
    4. splitterSizes << topWidgetHeight << 0;
    5. ui->splitter->setSizes(splitterSizes);
    6. ui->splitter->handle(1)->setUpdatesEnabled(false); // hides the handle - it can't repaint - setHidden(true) doesn't work
    7. bottomWidgetHidden = true;
    To copy to clipboard, switch view to plain text mode 

    on_button_Click() method:
    Qt Code:
    1. void MainWindow::on_ButtonClicked()
    2. {
    3. if (bottomWidgetHidden)
    4. {
    5. // show bottom widget
    6. int topWidgetHeight = ui->topWidget->height();
    7. mainWindowSize = this->size();
    8.  
    9. splitterSizes.clear();
    10. splitterSizes << topWidgetHeight << bottomWidgetHeight;
    11. ui->splitter->setSizes(splitterSizes);
    12.  
    13. ui->splitter->handle(1)->setUpdatesEnabled(true); // shows the handle
    14. this->resize(mainWindowSize.width(), mainWindowSize.height() + bottomWidgetHeight);
    15. }
    16. else
    17. {
    18. // hide bottom widget
    19. // get sizes/heights
    20. mainWindowSize = this->size();
    21. int topWidgetHeight = ui->topWidget->height();
    22. bottomWidgetHeight = ui->bottomWidget->height();
    23.  
    24. // set the widget heights - this maintains the size of the top widget
    25. splitterSizes.clear();
    26. splitterSizes << topWidgetHeight << 0;
    27. ui->splitter->setSizes(splitterSizes);
    28.  
    29. ui->splitter->handle(1)->setUpdatesEnabled(false); // hides the handle - it can't repaint - setHidden(true) doesn't work
    30. this->resize(mainWindowSize.width(), mainWindowSize.height() - bottomWidgetHeight);
    31. }
    32. bottomWidgetHidden = !bottomWidgetHidden;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Cheers
    Jeff

  6. #6
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Toggling the size of a window

    Good to hear it works
    Dong Back Kim

Similar Threads

  1. Get window size prior to showing window?
    By mortoray in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2011, 16:52
  2. Replies: 4
    Last Post: 20th November 2009, 12:25
  3. Window Size
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2009, 13:02
  4. How to avoid keyboard-button-toggling ?
    By Comer352l in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 11:02
  5. Replies: 4
    Last Post: 23rd November 2006, 05:24

Tags for this Thread

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.