QT graphical interface development in Linux (1) -- Overview (1)

Source: Internet
Author: User
Tags qt designer

 

I. Introduction to QT

Qt is a cross-platform C ++ graphical user interface library ,. developed by TrollTech, Norway, it currently includes Qt/X11, Qt Embedded based on Frambuffer, and Qt Designer, a quick development tool. qt supports Unix and Linux systems and Windows NT/Windows 2 K and Qindows 95/98 platforms. the excellent encapsulation mechanism of Qt makes it highly modular and reusable.

Qt/Embeded is a Qt version for embedded systems. It is a Qt embedded Linux window and a complete self-contained C ++ GUI and Linux-based embedded platform development tool. The Qt/Embeded API can be used for multiple development projects. Many Qt-based X Window programs can be easily transplanted to embedded versions.

Next we will understand Qt from practical applications.

2. Compile the Qt program on the terminal

First, we will introduce how to compile the Qt program in the terminal window.

Open a terminal and create a folder, such as mkdir hello

Then go to the folder: cd hello

Create a c ++ file, vi hello. cpp

Type the following code in the file:

# Include <qapplication. h>

# Include <qlabel. h>

Int main (INT argc, char * argv [])

{

Qapplication app (argc, argv );

Qlabel * label = new qlabel ("Hello QT! ", 0 );

App. setmainwidget (Label );

Label-> show ();

Return app.exe C ();

}

Then we use the qt tool qmake to generate the project file:

Qmake-project

You can use ls to check whether there is an additional project file named hello. pro.

Next, generate the Makefile file:

Qmake hello. pro

Haha, just a moment, Makefile will finally see your.

The next step is make. Wait for a while.

Run the program./hello

The following is the interface:

Next we will explain the above program:

# Include <qapplication. h>

This line contains the definition of the QApplication class. Each application that uses Qt must use a QApplication object. QApplication manages a wide variety of application resources, such as default fonts and cursors.

# Include <qpushbutton. h>

This line contains the definition of the QPushButton class. The top part of the document references the description of which header file must be included when a class is used.

QPushButton is a classic graphical user interface button. You can press it or open it. It manages its own perception, just like every other QWidget. A widget is a user interface object that can process user input and draw images. Programmers can change the overall perception and many of its main attributes (such as color), as well as the content of this widget. A QPushButton can display a piece of text or a QPixmap.

Int main (int argc, char ** argv)

The main () function is the entry of the program. In almost all cases where Qt is used, main () only needs to perform initialization before the control is transferred to the Qt library, and then the Qt library notifies the program of the user's behavior through events.

Argc is the number of command line variables, and argv is the array of command line variables. This is a C/C ++ feature. It is not proprietary to Qt. In any case, Qt needs to process these variables (see below ).

QApplication a (argc, argv );

A is the QApplication of this program. It is created and processed here. It is required to create a QApplication object before any Qt Windows system component is used.

QPushButton hello ("Hello world! ", 0 );

Here, after QApplication, the first window system code is followed: a button is created.

This button is set to display "Hello world !" And it forms a window (because 0 is specified as its parent window in the constructor, the button is located in this parent window ).

Hello. resize (100, 30 );

This alcohol-based setting is 100 pixels wide and 30 pixels high (with the window system border added ). In this case, we do not need to consider the button location, and we accept the default value.

A. setMainWidget (& hello );

This button is selected as the main widget of the application. If you close the Main Window widget, the application exits.

You don't have to set a Main Window widget, but most programs have one.

Hello. show ();

It is invisible when you create a widget. You must call show () to make it visible.

Return a.exe C ();

Here, main () transfers the control to QT, and when the application exits, exec () will return.

In exec (), QT accepts and processes user and system events and passes them to appropriate widgets.

Http://blog.chinaunix.net/space.php? Uid = 9532036 & Do = Blog & id = 2403540

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.