PDA

View Full Version : sizeHint()



sm
19th January 2008, 20:15
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.

Gopala Krishna
20th January 2008, 10:16
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

wysota
20th January 2008, 20:49
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 Expanding Dialog 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.

jpn
21st January 2008, 06:40
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.

wysota
21st January 2008, 11:51
Yes, that's true. I meant that it has no relation to the size policy set. A bit unclear of me, I admit :)

sm
21st January 2008, 15:57
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.

jpn
21st January 2008, 17:06
You would reimplement the virtual function in normal C++ way:


// mywidget.h
class MyWidget : public QWidget
{
public:
...
QSize sizeHint() const;
...
};




// mywidget.cpp
QSize MyWidget::sizeHint() const
{
return QSize(200, 200);
}

sm
21st January 2008, 17:56
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?



adjustSize();

or


QSize sizeHint();
adjustSize();


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()?

jpn
21st January 2008, 18:12
QSize sizeHint();
adjustSize();


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.

sm
21st January 2008, 18:53
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).

wysota
21st January 2008, 22:48
"QSize sizeHint();" probably compiles as a "declaration of a local function sizeHint() returning QSize".

Gopala Krishna
22nd January 2008, 03:59
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.