Results 1 to 12 of 12

Thread: sizeHint()

  1. #1
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default sizeHint()

    I have a basic question about sizeHint. I have read thru the Qt documentation but I still don't know how to use sizeHint. Never have I been able to compile anything that has sizeHint() in it. I'd like to know what is the proper use of sizeHint.
    For example if I want sizeHint to take care of the size of my application's window, how do I use it? Let's assume that I want the size always to be between 200x200 and 300x300 but as small as possible at the same time. What does the command look like when in action? I have not been able to find any relevant exaples online either. Thanks for help.

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: sizeHint()

    Quote Originally Posted by sm View Post
    I have a basic question about sizeHint. I have read thru the Qt documentation but I still don't know how to use sizeHint. Never have I been able to compile anything that has sizeHint() in it. I'd like to know what is the proper use of sizeHint.
    For example if I want sizeHint to take care of the size of my application's window, how do I use it? Let's assume that I want the size always to be between 200x200 and 300x300 but as small as possible at the same time. What does the command look like when in action? I have not been able to find any relevant exaples online either. Thanks for help.
    You can use QWidget::minimumSizeHint() to return the minimum recommended size for your window and QWidget::sizeHint() to return the recommended size.

    These properties are used by QLayout's to take care of the geometry of the widgets. QLayout won't resize widgets lesser than it's min size hint.

    Also you can control how the widgets should behave in various circumstances(eg when there is extra space ) by using QSizePolicy.
    See also QWidget::sizePolicy
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. The following user says thank you to Gopala Krishna for this useful post:

    sm (21st January 2008)

  4. #3
    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: sizeHint()

    Quote Originally Posted by sm View Post
    I have a basic question about sizeHint. I have read thru the Qt documentation but I still don't know how to use sizeHint. Never have I been able to compile anything that has sizeHint() in it. I'd like to know what is the proper use of sizeHint.
    Size hint is used for widgets that are inside layouts thus for top level windows it is not used.

    For example if I want sizeHint to take care of the size of my application's window, how do I use it? Let's assume that I want the size always to be between 200x200 and 300x300 but as small as possible at the same time. What does the command look like when in action? I have not been able to find any relevant exaples online either. Thanks for help.
    If you want to use the size hint for top level windows, you'll have to ask the window's layout to enforce a size constraint on its parent - just like the [WIKI]Expanding Dialog[/WIKI] article explains. But remember that size hint only tells the layout what the suggested size of a particular widget is. If you wish to enforce a range of sizes, it might be simplest to reimplement its resizeEvent and make sure the size stays within specified bounds.

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

    sm (21st January 2008)

  6. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sizeHint()

    Quote Originally Posted by wysota View Post
    Size hint is used for widgets that are inside layouts thus for top level windows it is not used.
    When the window is shown, sizeHint() has effect to its initial size.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    sm (21st January 2008)

  8. #5
    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: sizeHint()

    Yes, that's true. I meant that it has no relation to the size policy set. A bit unclear of me, I admit

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

    sm (21st January 2008)

  10. #6
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sizeHint()

    Thanks a lot to all of you. It is now much more clear to me how sizeHint works. But I'd really appreciate it is someone could send me a two line example of how to implement sizeHint. What is the syntax? For example if I want sizeHint to be 200x200 pixels, how do I write it?
    Documentation says: "virtual QSize sizeHint () const" but I can't figure out how to use it. Thanks again.

  11. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sizeHint()

    You would reimplement the virtual function in normal C++ way:
    Qt Code:
    1. // mywidget.h
    2. class MyWidget : public QWidget
    3. {
    4. public:
    5. ...
    6. QSize sizeHint() const;
    7. ...
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // mywidget.cpp
    2. QSize MyWidget::sizeHint() const
    3. {
    4. return QSize(200, 200);
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    sm (21st January 2008)

  13. #8
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sizeHint()

    Great!
    I was able to implement sizeHint like you suggested without the compiler complaining about it (for the first time). If I want to resize the main window, do I just do this?

    Qt Code:
    1. adjustSize();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QSize sizeHint();
    2. adjustSize();
    To copy to clipboard, switch view to plain text mode 

    Both of these only work abot half the time.
    My question is how does another function that uses sizeHint() actually know to invoke the sizeHint() function? How do you call sizeHint() when you want to use it for adjustSize()?

  14. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: sizeHint()

    Quote Originally Posted by sm View Post
    Qt Code:
    1. QSize sizeHint();
    2. adjustSize();
    To copy to clipboard, switch view to plain text mode 
    The first one is not a valid C++ statement. It shouldn't even compile.

    From QWidget::adjustSize() docs:
    Adjusts the size of the widget to fit the contents.

    Uses sizeHint() if valid (i.e if the size hint's width and height are >= 0); otherwise sets the size to the children rectangle that covers all child widgets (the union of all child widget rectangles). For top-level widgets, the screen size is also taken into account.
    As it says, adjustSize() will use the value returned by sizeHint().

    Edit: PS. Oh, actually it is a valid C++ statement. It just constructs a variable of type QSize called sizeHint. However, it won't call that function.
    Last edited by jpn; 21st January 2008 at 19:11. Reason: updated contents
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    sm (21st January 2008)

  16. #10
    Join Date
    Jan 2008
    Posts
    7
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sizeHint()

    For some reason both of the versions compiled. I am now only using the version that you suggested (the second version). But even this only works but only half the time. I understand that sizeHint() + adjustSize() are only executed when they are possible (the size is meaningful) but in my case they don't work when it should be very easy to do what's asked. I know it's very difficult for anyone to trouble-shoot my code without seeing it. So I have to figure it out myself. In order to do this I'd like to know the following:
    a) do you have to put the adjustSize() in in the end of the drawing functions (only execute it after everything else has been done)?
    b) is it OK to put adjustSize() in any function (other than main function)
    c) how do the addStretch commands in they layout constructor affect adjustSize? is it possible that addStretch functions make adjustSize somewhat nonfunctional because they tend to expand the layout (while I try to use adjustSize to contract it).
    Last edited by sm; 21st January 2008 at 21:13.

  17. #11
    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: sizeHint()

    "QSize sizeHint();" probably compiles as a "declaration of a local function sizeHint() returning QSize".

  18. #12
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: sizeHint()

    It would be nice if you could post some code.
    BTW sizeHint is used by layouts mainly and if you are using layout then it is enough to just reimplement sizeHint() as suggested. The rest is taken care by the layout.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. sizeHint
    By eric in forum Newbie
    Replies: 1
    Last Post: 8th January 2008, 16:18
  2. How to set the sizehint for treeWidget
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 11:35
  3. QTextEdit, sizeHint, QWidget
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2007, 08:25
  4. invalid sizeHint()
    By roleroz in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 22:11

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.