The use of Qt introductory learning--QT Creator

Source: Internet
Author: User
Tags error code qt designer
Introduction to Qt creator

Through the previous "QT QuickStart (Vim Pure Code writing)" Learning that only set up the QT environment (QT Library and development tools), you can use vim pure code to write QT program, and then use QT qmake tools to compile QT code, the specific process is as follows:

1) Writing Qt code

2) Generate project files through Qmake-project

3) Add the required modules to the project file: QT + = Widgets

4) Generate Makefile with qmake command according to the project file

5) Generate required executable program by make compile code

6) Run the program through "./"


However, if the QT program is relatively large, through the above way to write more cumbersome (greatly affect the writing efficiency), next to introduce you to the QT Integrated development Environment (IDE)--QT Creator, the use of QT Creator can be more rapid and easy to complete Qt Development Tasks . What we need to note here is thatQT Creator only assists us in the rapid development of QT programs, not that the development of QT program is not to qt Creator .


Qt Creator is a new cross-platform QT IDE (integrated development environment) that can be used alone or with the QT Library and development tools to form a complete SDK (software Development Kit). These include: Advanced C + + code Editor, project and build management tools, integrated context-sensitive Help system, graphical debugger, code management and browsing tools.


The following is the Qt Creator interface diagram, different versions vary (not very small):



1) Welcome mode (Welcome): Here you can choose to bring your own example to show that the next time you open the Welcome screen, you will be able to display some of the most recent items.

2) compilation mode (Edit): Write code here for programming.

3)Designer mode (design): A graphical interface is designed here to perform such operations as part property setting, signal and slot setting, and layout setting.

4) Debugmode (Debug): In this interface you can debug the program as needed in order to track the operation of the Watcher.

5) Project Setup (Projects): This interface allows you to complete the configuration of the development environment.

6) help mode: You can enter keywords here to find relevant information.


The following example is tested in Ubuntu environment, because QT and QT Creator have cross-platform features , so if Windows also installed QT Creator (see "QT 5.5.0 Windows Environment Building"), in The QT code written under Ubuntu can be completely unmodified, that is, the QT Creator compilation runs in Windows. Similarly, the Qt code written in Windows can be compiled and run under Ubuntu, please choose your own platform.


Create a blank project

Sometimes, if we use someone else's well-written QT code, this code may not include the project file (sample code download please click this link: Http://download.csdn.net/detail/tennysonsky), at this time, we need to use QT Creator Create a blank project file and add the required information.


1) Right-click File--New file or project



2) Other projects, Empty Qmake project



3) Fill in the project name and choose the storage path. The new project automatically creates a folder with a. Pro project file with the same name as the folder.



4) Next



5) Complete



6) in edit mode, select the project directory right click, "Add Existing File", add the required files (mainly. cpp and. h files)


7) In the edit mode, add the corresponding module information in the project file, for example:QT + = Widgets



8) After saving the compilation run, select the project directory right click, "Run", you can compile and run the program



Qt Creator Regardless of the way in which the project files are created, the directory where the project files are located automatically generates a user profile with the suffix. Username, which mainly holds the configuration information for this project (e.g., the path to the project, some error messages when compiling the code), so If you want to copy the code you write to others to use, it is best to delete this configuration file .



Let's teach you how to quickly open the directory where the project is located:

Select "Show included Directories" in edit mode, select Project file, right click, to display the directory where the project is located (this method can also open the directory where the code file is located):



after the QT code is run by Qt Creator, a matching buid-directory is generated in the catalog's sibling directory :



enter this build-directory to find executable programs that can be run directly through "./" at the terminal :


Build Gui Application complete Project Create a complete project (without UI)

Next, we create a GUI application complete project (without the UI, as for what the UI, described later), we can create a blank window without writing a line of code at all.


1) Right-click File--New file or project



2) application, Qt Widgets application



3) Fill in the project name and select the storage path



4) Next



5) Fill in the class name of the custom class, select the desired base class (Qmainwindow, Qwidget, Qdialog), where the base class is selected as Qwidget. By default, the Create Interface check box is selected to indicate that you need to design the interface with your own interface designer, otherwise you need to use the code to complete the design of the interface, which is not selected here .

The Qmainwindow class provides a main application window with a menu bar, an anchor window (such as a toolbar), and a status bar.

The Qwidget class is the base class for all user-interface objects.

The Qdialog class is the base class for dialog Windows. A dialog window is a top-level window that is primarily used for short-term tasks and for brief communication with users.



6) Complete



7) Compile and run the program, click the green right arrow in the lower left corner, or press the shortcut key "Ctrl + R" to run the result as a blank window.



We did not write a single line of code to create a blank window, if you want to add a button on the window, you need to create a button object in the. h file custom class, corresponding to the. cpp file constructor, the corresponding property settings for the button, the specific operation and "QT QuickStart (Vim Pure Code writing)" the same.


Here are some common shortcut keys:

1) Help file:F1 (cursor on the function name or class name, press F1 to jump to the corresponding help document to see its detailed usage)

2). h file and corresponding . cpp file switch:F4

3) Compile and run:Ctrl + R

4) function declaration and definition switch:F2

5) Code comment uncomment:Ctrl +/ (check the code and press the shortcut key again)

6) font size becomes smaller:Ctrl + mouse wheel up and down


Create a complete project (with UI)

Operation and creation of a complete project (without UI) is basically the same, the main difference is that in the fifth step, the default is to Select the "Create Interface" check box, indicating the need to use the interface designer to design the interface, otherwise you need to use the code to complete the design of the interface , which is selected by default .



Next, you can find that the code directory structure is more than one interface file column: Mywidget.ui, which is the UI file (interface file), this file is actually an XML file.



Double-click the UI file to enter design mode, and go to the designer QT designer editing state to begin the design (QT designer) programming .




In the process of QT program development, the process of software development through handwritten code, but also through the Qt interface designer (QT Designer) interface drawing and layout . The interface designer (QT Designer) provides QT basic drawing widgets, which can be dragged and dragged through the mouse directly in the designer, enabling the design of graphical interfaces efficiently and quickly, with an intuitive interface and WYSIWYG. This has the advantage of being able to visually see the components on the interface at the same time, and to adjust the design of the interface at any time.


To add the required widgets to the UI:



Set the part properties as needed:




Qt Designer Most of the operations are similar, need their own multi-operation, multi-validation, here is not an example of the explanation.


after you have set up the graphical interface through Qt Designer, you can compile and run the program, the same steps as before .


If the program has been compiled, not modified to compile, the program will no longer perform the compilation process, immediately run. If the program does not modify and you want to perform the compilation action, you need to clear and then compile the run:

1) design mode switch to edit mode



2. In edit mode, select the project directory, right-click, clear, run



When the program compiles, it can see its corresponding compilation information through the "4 compile output" in the bottom of the interface:



If the program runs successfully, "3 application Output" will be viewed at the bottom of the interface to see the program output status, and if the program has a print operation, print information will be output here:





If there is a problem with the program code, compile error, the interface at the bottom of the "1 problem" input error message, double-click the information to jump to the error code:



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.