QT Learning Pathway (5): Component layout

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.