As the name suggests, absolute positioning is the use of the most original positioning method, given the coordinates of this component and the length of the value. In this way, Qt knows where to put the components and how to set the size of the components. But one of the problems with this is that if the user changes the window size, such as clicking on maximizing or dragging the edge of the window, then you have to write the appropriate function to respond to these changes to avoid those components still in a quiet corner. Alternatively, a simpler way is to prevent users from changing their size directly.
However, QT provides another mechanism, the layout, to solve this problem. You just put the component in a certain layout, and Qt knows how to adjust it when it needs to be resized or positioned. This is similar to swing's layout manager, but the QT layout is not that much, only a limited number.
Take a look at the following example:
#include <qtgui/qapplication>
#include <qtgui/qwidget>
#include <QtGui/QSpinBox>
#include <qtgui/qslider>
#include <qtgui/qhboxlayout>
int main (int argc, char *argv[])
{
Qapplication app (argc, argv);
Qwidget *window = new Qwidget;
Window->setwindowtitle ("Enter your Age");
Qspinbox *spinbox = new Qspinbox;
Qslider *slider = new Qslider (qt::horizontal);
Spinbox->setrange (0, 130);
Slider->setrange (0, 130);
Qobject::connect (Slider, SIGNAL (valuechanged (int)), Spinbox, SLOT (int));
Qobject::connect (Spinbox, SIGNAL (valuechanged (int)), slider, SLOT (int));
Spinbox->setvalue (35);
Qhboxlayout *layout = new Qhboxlayout;
Layout->addwidget (Spinbox);
Layout->addwidget (slider);
Window->setlayout (layout);
Window->show ();
return app.exec ();
}
Two new components are used here: Qspinbox and Qslider, and a new top-level window qwidget. Qspinbox is a spinner with up and down arrows, Qslider is a slider, so just run it and you'll see what it is.
The code is not so difficult to understand, or to simply look at it. First, a Qwidget instance is created, and the Setwindowtitle function is invoked to set the window caption. It then creates a qspinbox and Qslider, sets the range of their values, and uses the SetRange function. Then make a link to the signal slot. This point is explained in detail later. Then there is a qhboxlayout, which is a horizontal layout, added from left to right, using AddWidget to add a component, calling Qwidget's setlayout to set Qwidget layout as the layout we defined, So, the program is done!
Compile to run, you can see the effect:
If maximized: