Results 1 to 7 of 7

Thread: How to resize a window after having added widgets at runtime

  1. #1
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to resize a window after having added widgets at runtime

    Hello,

    I have a window (it's a QWidget, not a main window) that contains a QScrollArea, that itself contains a QFormLayout with widgets in it.

    The following scenario works: I populate the QFormLayout with widgets during the construction of the window, then I resize the window using resize(sizeHint()), and finally I present the window using showNormal() followed by activateWindow().

    Now, I'm trying to add more widgets to the QFormLayout at runtime, after the window was presented. The widgets appear correctly, but the resize(sizeHint()) trick doesn't enlarge the window; instead, the QScrollArea now shows a vertical scrollbar.

    What it the correct way to basically tell the QScrollArea to *not* show a scrollbar, but to instead resize the window so that all the widgets become visible?

    Cheerz,
    Franz

  2. #2
    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: How to resize a window after having added widgets at runtime

    If you don't want QScrollArea to show a scroll bar then why are you using QScrollArea in the first place? The whole point of this widget is to show scrollbars when content doesn't fit onto the display.
    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.


  3. #3
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default

    Sorry, I wasn't clear on this point. I'm using a QScrollArea because (1) I want the user to be able to resize the window freely, possibly to a size that cannot accomodate all the widgets, and (2) there might be way too many widgets to display, so at some point I might have to limit the height of the window anyway, and let the user scroll through the widgets using the scrollbars.

    To illustrate a bit my problem:



    Cheerz,
    Franz
    Last edited by wysota; 1st September 2010 at 15:00.

  4. #4
    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: How to resize a window after having added widgets at runtime

    Quote Originally Posted by dictoon View Post
    Sorry, I wasn't clear on this point. I'm using a QScrollArea because (1) I want the user to be able to resize the window freely, possibly to a size that cannot accomodate all the widgets, and (2) there might be way too many widgets to display, so at some point I might have to limit the height of the window anyway, and let the user scroll through the widgets using the scrollbars.
    So you can't possibly expect sizeHint() to do what you want if you want to allow widgets to be scrolled. You can calculate the proper height yourself but don't expect Qt to do it for you. And expect users to be mad when they resize the window and after a while you change its size to the default one from within your program's code.

    To illustrate a bit my problem:
    So get rid of the scroll area until you need it and everything will work out of the box.
    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.


  5. #5
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to resize a window after having added widgets at runtime

    Quote Originally Posted by wysota View Post
    So you can't possibly expect sizeHint() to do what you want if you want to allow widgets to be scrolled. You can calculate the proper height yourself but don't expect Qt to do it for you. And expect users to be mad when they resize the window and after a while you change its size to the default one from within your program's code.
    Users won't get "mad" because I only plan to increase the height of the window if the current height is too small to accomodate the new widgets (and I'm never changing the width). What would be a better way to present a variable number of widgets in a window?

    Quote Originally Posted by wysota View Post
    So get rid of the scroll area until you need it and everything will work out of the box.
    I thought of that but, unless I'm mistaking, I still need the scroll area in case the user resizes the window to a smaller size.

    Look, I'm not expecting Qt to do the magic for me. I'm just a bit confused about what is the proper way to adjust the height of my window after having added widgets at runtime. I basically want to simulate what Qt does when I present the window with its initial set of widgets: in this case, Qt has no problem to resize the window appropriately (because sizeHint() returns the right value).

    Thanks for your help.

    Cheers,
    Franz

  6. #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: How to resize a window after having added widgets at runtime

    Quote Originally Posted by dictoon View Post
    Users won't get "mad" because I only plan to increase the height of the window if the current height is too small to accomodate the new widgets (and I'm never changing the width).
    And if I (as a user) want to reduce the height of the window on purpose and you constantly keep increasing it back? I really hate when programmers are trying to be smarter than users of their software...

    What would be a better way to present a variable number of widgets in a window?
    If you have a scroll are then rely on it and the sanity of the user.


    I'm just a bit confused about what is the proper way to adjust the height of my window after having added widgets at runtime.
    The proper way is to use layouts that will do it for you.
    because sizeHint() returns the right value
    The thing is sizeHint() returns the right value all the time. If you add a widget to the scroll area inside the controlled widget, it doesn't cause a necessity to increase the window's size, it's just you as the programmer who sees this necessity. There is nothing stopping you from reimplementing sizeHint() for your widget and defining it differently. Actually you should provide your own layout class here but I really don't see how would you detect that you shouldn't be increasing the window size anymore and should allow the scroll area to do its job...
    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.


  7. #7
    Join Date
    Dec 2009
    Posts
    49
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to resize a window after having added widgets at runtime

    Quote Originally Posted by wysota View Post
    And if I (as a user) want to reduce the height of the window on purpose and you constantly keep increasing it back? I really hate when programmers are trying to be smarter than users of their software...

    If you have a scroll are then rely on it and the sanity of the user.
    Fair enough. Maybe I'm just too focused on this issue because the initial size of the window is too small, and I should rather make the window larger initially and leave the user in full control of the window's dimensions after that...

    Quote Originally Posted by wysota View Post
    The proper way is to use layouts that will do it for you.

    The thing is sizeHint() returns the right value all the time. If you add a widget to the scroll area inside the controlled widget, it doesn't cause a necessity to increase the window's size, it's just you as the programmer who sees this necessity. There is nothing stopping you from reimplementing sizeHint() for your widget and defining it differently. Actually you should provide your own layout class here but I really don't see how would you detect that you shouldn't be increasing the window size anymore and should allow the scroll area to do its job...
    I see. Thanks for the heads-up.

    Cheerz,
    Franz

Similar Threads

  1. How can I resize QWidgets added to a QTabWidget
    By MorrisLiang in forum Newbie
    Replies: 1
    Last Post: 29th May 2010, 11:44
  2. delete runtime-created widgets causes crash
    By jonasbalmer in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2010, 21:36
  3. Replies: 1
    Last Post: 20th July 2008, 19:47
  4. Set a window as child at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2007, 09:30
  5. How to remove widget added in runtime?
    By jiveaxe in forum Qt Programming
    Replies: 7
    Last Post: 2nd November 2007, 10:13

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.