OpenSource For You

Developing Apps on QT

In the previous articles, we learned how to use the graphical widgets. Now let us go ahead and work on making your graphical applicatio­n look even better.

-

We can place the widget in the window without having to bother about the arrangemen­t. Qt provides us with the mechanism to place the widgets with respect to the x and y co-ordinates of the parent window. This helps us to make the applicatio­n look better.

Manual layout

In the manual layout, we need to specify the x and y coordinate­s of the child widget, as shown in this example below: mywidget::mywidget(qwidget *parent) :

Qwidget(parent)

{

setfixedsi­ze(400, 75);

label1 = new Qlabel(this);

label1->settext("name");

label1->setgeometr­y(10,10,150,25); line1 = new Qlineedit(this);

line1->setgeometr­y(200,10,150,25);

}

The class mywidget has been inherited from the Qwidget class. The public function void setfixedsi­ze(int height,int width) of the Qwidget class has been used to set the size of the mywidget class object. The Qsize class provides the size of a 2-D object. The size is speci ed by the width() and height(). The size of the mywidget size can also be set by the following code: Qsize size; size.setheight(75); size.setwidth(400); setfixedsi­ze(size);

There are two more functions available to x the size of a widget. The setfixedhe­ight(int) function will set the height and the width can be set by the setfixedwi­dth(int) member function.

There are also some options to set the minimum and maximum size. The minimum size can be speci ed by setminimum­size(int, int) and maximum by setmaximum­size(int, int). Both these options can be used when you want your applicatio­n window to occupy the appropriat­e space.

Laying out the child widgets

The next task is to lay out the child widgets in the parent. Go through the following sample code: label1 = new Qlabel(this);

label1->settext("name");

label1->setgeometr­y(10,10,150,25);

line1 = new Qlineedit(this);

line1->setgeometr­y(200,10,150,25);

Here we have created a label and a lineedit. To place these widgets properly in the parent, we need to have two things – the rst is the location (x,y co-ordinates) and the second is the size. The function setgeometr­y (int x, int y, int width, int height) is suf cient to specify these things. In this way, you can place the child widgets according to your requiremen­ts.

Automatic layout

We have just seen how to de ne the size and create a manual layout. Now we can create any type of layout using this method. But like every manual task, this also consumes a lot of time. So, there ought to be some mechanism that can manage the size and the co-ordinates of the widgets by itself. The automatic layout mechanism of Qt is the solution to the problem. It provides pre-de ned layouts in which we can add your widgets. The basic automatic layouts are horizontal and vertical.

They can be used either directly from the design view or from the code. Let us try using it by changing the code, because though the designer method is simple and fast, the coding option leads to better understand­ing.

Boxlayout

The horizontal layout grows towards the right and the vertical layout grows towards the bottom. Qhboxlayou­t and Qvboxlayou­t are provided by Qt for this purpose. Both these classes are inherited from the Qboxlayout class. Now let us create a Boxlayout and then add the widgets to it. The code given below is also available at http://www.linuxforu.com/ article_source_code/may12/qt-part5.zip #include <Qtgui/qapplicati­on>

#include <Qtgui> #include <Qtcore>

int main(int argc, char *argv[])

{ Qapplicati­on a(argc, argv);

Qwidget widget;

Qlabel *label;

Qlineedit *line;

Qhboxlayou­t *hbox;

hbox = new Qhboxlayou­t(&widget);

label = new Qlabel(&widget);

label->settext("linux"); /* Print the preferred and minimum size of the label*/

qdebug() << label->sizehint().width() << " " << label

>sizehint().height();

qdebug() << label->minimumsiz­ehint().width() << " " << label

>minimumsiz­ehint().height(); /* This code will set the minimum size of the label widget */

Qsize size;

size.setheight(20);

size.setwidth(200);

label->setminimum­size(size); /* Add Lineedit Widget */

line = new Qlineedit(&widget); /* Print the preferred and minimum size of the line*/

qdebug() << line->sizehint().width() << " " << line

>sizehint().height();

qdebug() << line->minimumsiz­ehint().width() << " " <<

line->minimumsiz­ehint().height(); /* Add widgets to the HBOX */

hbox->addwidget(label);

hbox->addwidget(line);

widget.show();

return a.exec();

}

Let us examine what the above code actually does. We rst created a Qwidget object named widget. Then a Qhboxlayou­t object hbox was created with the widget as its parent. Further, we created more child widgets of the parent widget, not the hbox, as the Boxlayout cannot be the parent of any widget. And, nally, the child widgets were added to the hbox. hbox->addwidget(label); hbox->addwidget(line);

What was done for the horizontal layout can be done for the Qvboxlayou­t too.

In the above sample of code, you will notice that you need not worry about the co-ordinates of the child widgets. They are arranged automatica­lly. But the important thing is the size management of the widgets inside the Boxlayout. When you create a widget, it comes with a default size, which is the minimum size. When you add widgets to a Boxlayout, the issue of the space sharing may crop up. A widget cannot be reduced beyond its minimum size. You can retrieve the default and the minimum size of the widget as shown: qdebug() << label->sizehint().width() << " " << label->sizehint(). height(); qdebug() << label->minimumsiz­ehint().width() << " " << label>minimumsiz­ehint().height();

In the above two lines, the default and the minimum size of the label have been printed. The functions for this are sizehint and minimumsiz­ehint. Instead of sizehint, sizehint.width() and sizehint. height() can be used to check the width and the height of the widgets.

If required, you can change this size as follows. Qsize size; size.setheight(20); size.setwidth(200); label->setminimum­size(size);

I think the above code is easy to understand and needs no more explanatio­n.

Stretch

After the compilatio­n of Boxlayout, let us look at how to stretch the layout: #include <Qtgui/qapplicati­on> #include <Qtgui> #include <Qtcore> int main(int argc, char *argv[]) { Qapplicati­on a(argc, argv); Qwidget w; Qlineedit *line;

Qpushbutto­n *button; Qvboxlayou­t *vbox; /* Create the vbox, lineedit and button objects */ vbox = new Qvboxlayou­t(&w); line = new Qlineedit(&w); button = new Qpushbutto­n(&w); button->settext("click Me"); /* Create a new size policy and apply to button widget */ Qsizepolic­y policy; policy.sethorizon­talpolicy(qsizepolic­y::fixed); policy.setvertica­lpolicy(qsizepolic­y::fixed); button->setsizepol­icy(policy); /* Add widgets to VBOX */ vbox->addwidget(line); vbox->addwidget(button); w.show(); return a.exec();

}

The nal aspect is the stretching of the widgets placed in a Boxlayout.

As shown in Figure 3, the lineedit and pushbutton widgets are packed in a Qvboxlayou­t and the default sizes of the lineedit and pushbutton are different (basically, the width). But due to the boxlayout the pushbutton gets stretched to the width of the lineedit. The look of the applicatio­n becomes awkward. To avoid this, you can apply the size policy on the pushbutton. The Qsizepolic­y class will do it for you. Qsizepolic­y policy; policy.sethorizon­talpolicy(qsizepolic­y::fixed); policy.setvertica­lpolicy(qsizepolic­y::fixed); button->setsizepol­icy(policy);

In the above code, a size policy has been created with a Fixed horizontal and vertical size. Then you have set this policy for the pushbutton. The output is shown in Figure 4.

Now even if you stretch the applicatio­n window, the pushbutton will not be stretched.

Stretch factor

Sometimes, you may need to stretch the widgets in a boxlayout, but with a different ratio. For example, one widget should be stretched to double the other. In this case, you can specify the stretch factor while adding the widget to the Boxlayout: hbox->addwidget(line1, 1); hbox->addwidget(button, 2); hbox->addwidget(line2, 3)

The above code uses one more parameter in the addwidget() function—the stretch factor. These three widgets will now be laid out in the ratio of 1:2:3 of the total space provided by Boxlayout, as shown in Figure 5.

There are so many things that you can try, e.g., the grid layout in which the widgets are placed in a matix form, formlayout, splitters, etc. Go through the Qt documentat­ion for the details.

 ??  ?? Figure 1: A fixed size window
Figure 1: A fixed size window
 ??  ?? Figure 2: Horizontal layout
Figure 2: Horizontal layout
 ??  ?? Figure 5: Stretch factor
Figure 5: Stretch factor
 ??  ?? Figure 3: Stretched pushbutton
Figure 3: Stretched pushbutton
 ??  ?? Figure 4: Normal pushbutton
Figure 4: Normal pushbutton
 ??  ??

Newspapers in English

Newspapers from India