QT Introductory-layout layout

Source: Internet
Author: User

To develop a graphical interface application, the layout of the interface affects the aesthetics of the interface. Before designing an interface, it should be considered that the development interface may be used by the user, while the user's screen size, aspect ratio, resolution may be different, the interface may be scalable, the program should be able to adapt to these changes.

This method is clumsy when you use the Setgeometry () method to position the control in the previous program. Imagine that if there are many controls, the layout of these controls requires a lot of code to write. Fortunately, QT provides a better way to layout controls.

Three common layout methods:

(1) Using Horizontal layout class qhboxlayout;

(2) using Vertical layout class qvboxlayout;

(3) Use Grid Layout class Qgridlayout.

These three methods can be used in nested nesting.

The control can be laid out without specifying a parent window, and finally assigned to layout uniformly.

Example:

#include <QApplication> #include <QDialog> #include <QPushButton> #include <QLineEdit> #
	Include <QLayout> #include <QLabel> int main (int argc, char *argv[]) {qapplication A (argc, argv);

	Qdialog *mainwindow = new Qdialog;
	Qhboxlayout *toplayout = new Qhboxlayout;
	Qlabel *lbl = new Qlabel (Qwidget::tr ("&input:"), MainWindow);
	Qlineedit *lineedt = new Qlineedit (MainWindow);
	Lbl->setbuddy (Lineedt);
	Toplayout->addwidget (LBL);

	Toplayout->addwidget (Lineedt);
	Qhboxlayout *bomlayout = new Qhboxlayout;
	Qpushbutton *BTN_OK = new Qpushbutton (qwidget::tr ("OK"), mainWindow); \ Btn_ok->setdefault (TRUE);
	Qpushbutton *btn_cancel = new Qpushbutton (Qwidget::tr ("Cancel"), MainWindow);
	Bomlayout->addstretch ();
	Bomlayout->addwidget (BTN_OK);
	Bomlayout->addstretch ();
	Bomlayout->addwidget (Btn_cancel);

	Bomlayout->addstretch ();
	Qvboxlayout *mainlayout = new Qvboxlayout;
	Mainlayout->addlayout (toplayout); Mainlayout->aDdlayout (bomlayout);

	Mainwindow->setlayout (mainlayout);
	Mainwindow->resize (300, 100);
	Mainwindow->setwindowtitle (Qwidget::tr ("Qt Test"));

	Mainwindow->show ();
return A.exec ();

 }


Compile and run with the following interface:

In the interface, the most external is mainlayout, whose type is the vertical layout class qvboxlayout. It contains two horizontal layout class qhboxlayout, namely Toplayout and Bomlayout.

Qgridlayout uses more flexibility than qhboxlayout and hvboxlayout.

Common methods of Qgridlayout

(1) AddWidget:

Place a control into a cell
void	addwidget (qwidget * widget, int row, int column, qt::alignment Alignment = 0)
//If the control is placed Out of a cell, use the method
void	addwidget (qwidget * widget, int fromrow, int fromcolumn, int rowSpan, int columnspan, Qt::al Ignment Alignment = 0)

1) Row: Refers to the grid row number where the control is placed (line number starts from 0);

2) Colum: Refers to the grid column number where the control is placed (starting from 0);

3) Alignment: Alignment.

4) Fromrow: Refers to the starting grid row number of the placement control;

5) Fromcolumn: Refers to the starting grid column number of the placement control;

6) RowSpan: How many rows the placement control occupies;

7) ColumnSpan: How many columns the placement control occupies.


(2) Addlayout

void	addlayout (qlayout * layout, int row, int column, qt::alignment Alignment = 0)
void	addlayout (qlayout * layout, int row, int column, int rowSpan, int columnspan, qt::alignment Alignment = 0)

The parameters are similar to AddWidget.

(3) setspacing

void qgridlayout::setspacing (int spacing)

Sets the interval between horizontal and vertical controls.

Example:

#include <QApplication> #include <QDialog> #include <QPushButton> #include <QLineEdit> # Include <QLayout> #include <QLabel> #include <QTextEdit> int main (int argc, char *argv[]) {Qapplicat
	Ion A (argc, argv);

	Qdialog *mainwindow = new Qdialog;
	Qgridlayout *gridlayout = new Qgridlayout;
	Gridlayout->setcolumnstretch (0, 1);
	Gridlayout->setcolumnstretch (1, 4);
	Gridlayout->setcolumnstretch (2, 1);
	Gridlayout->setcolumnstretch (3, 1);

	Gridlayout->setcolumnstretch (4, 4);
	Gridlayout->setmargin (15);


	Gridlayout->setcolumnminimumwidth (2, 15);
	Qlabel *lbl1 = new Qlabel (Qwidget::tr ("First Name:"));
	Qlineedit *edit1 = new Qlineedit;
	Qlabel *lbl2 = new Qlabel (Qwidget::tr ("Last Name:"));
	Qlineedit *edit2 = new Qlineedit;
	Qlabel *lbl3 = new Qlabel (Qwidget::tr ("Sex:"));
	Qlineedit *edit3 = new Qlineedit;
	Qlabel *lbl4 = new Qlabel (Qwidget::tr ("Birthday:"));
	Qlineedit *edit4 = new Qlineedit; Qlabel *lbl5 = new Qlabel (QWIDGEt::tr ("Address:"));



	Qtextedit *textedt = new Qtextedit;
	Gridlayout->addwidget (lbl1, 0, 0);
	Gridlayout->addwidget (edit1, 0, 1);
	Gridlayout->addwidget (LBL2, 0, 3);
	Gridlayout->addwidget (edit2, 0, 4);
	Gridlayout->addwidget (LBL3, 1, 0);
	Gridlayout->addwidget (EDIT3, 1, 1);
	Gridlayout->addwidget (LBL4, 1, 3);
	Gridlayout->addwidget (EDIT4, 1, 4);
	Gridlayout->addwidget (LBL5, 2, 0);
	
	Gridlayout->addwidget (Textedt, 3, 0, 2, 5);

	Mainwindow->setlayout (gridLayout);
	Mainwindow->resize (400, 150);
	Mainwindow->setwindowtitle (Qwidget::tr ("Qt Test"));

	Mainwindow->show ();
return A.exec ();

 }


Compile and run, the interface as shown:

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.