Understanding Form Layout Mechanisms in Qt

Qt has a flexible and powerful layout engine for handling window views of desktop applications.

But it is so flexible that it is almost impossible to understand when something goes wrong and needs to be fine-tuned. And so powerful that he can defeat anyone in his efforts to quell Qt's opinion on how the form should look.

So, can anyone explain or provide articles or a source for Qt's positioning mechanisms?

I am trying to force the QLabel , QPushButton and QTableView marked with underscores in their names are twice as high as QTextBrowser with verticalStretch = 1 below. How can I handle widget height correctly?

. The ui file of my form in google docs. Search "____" in names; preview in QtDesigner

+53
qt forms qt4 qt-designer
Dec 29 '10 at 10:29
source share
2 answers

Layouts are actually easy to understand "I think" :)
A simple explanation of the layouts can be found in the qt book " C ++ Programming with QT 2nd edition

What you need to know about layouts and their size policies

  • Most Qt widgets have a size policy. This size policy tells the system how the widget should stretch or contract. It got out of the QSizePolicy class. Size policy has both vertical and horizontal components.
  • Most widgets also have a size hint. This size hint tells the system about preferred widget sizes
  • QSizePolicy has a stretch factor that allows widgets to grow at different speeds.

I know only 4 size policies

  • fixed size policy. The widget size is fixed and cannot be stretched. It stays behind the size hint.
  • minimum size policy - size hint - this is the minimum possible size of the widget, but it can increase if necessary.
  • Preferred size policy - the widget can shrink or grow larger than the size hint.
  • size expansion policy - the widget may decrease or increase more than its tip :)

You can ask,

What is the difference between preferred and expanding?
Answer: Imagine a form with two widgets, one with a preferred one and the other with an extension. Then additional space is added for the expanding widget. The preferred policy widget will remain with a size hint.

I recommend (WARNING: not an expert :)) you buy and readC ++ programming using QT 2nd edition . I currently read it and make a lot of sense. Look at the images and see if they make sense.

Size Policies Explained
Size policy explained

Simple example
This is a simple two-button dialog for which horizontal and vertical dimension policies are shown, as well as horizontal and vertical stretching.

example

Here is the smallest preview. alt text

Here is another preview at a larger size alt text

[EDITED: // size hint example added]

WHY YOU SHOULD BE PROVIDED FOR ACCOMMODATION
You can see that each widget has sizeHint, which is vital because the QT layout system always respects sizeHint. This is only a problem if the default widget size is not quite what you want. The only way to solve this problem is to extend (subclass) the widget and reimplement its sizeHint () member function. An example is 1000 words. To save space, see my blog for an example project.

+105
Jan 12 '11 at 18:29
source share

You can use QT stylesheets to control widget heights and other properties in an easily customizable way.

http://doc.qt.io/archives/qt-4.7/stylesheet.html

As for layouts, you should use them wisely and heavily in combination with struts to make widgets behave exactly the way you want.

http://doc.qt.io/archives/qt-4.7/designer-layouts.html

-one
Dec 29 '10 at 13:10
source share



All Articles