QT Beginner's Hello World

Source: Internet
Author: User
Tags qt designer

The previous article I explained how to install the QT development environment, below we explain how to create a new project, the structure of the project consists of. Here we review some of the points of knowledge.


My development bad environment is Ubuntu16.04 TLS, we use the installation package screenshot below, one is Qt creater, one is qtlibary. Qtcreater is an integrated development environment, we do not have to, direct command line to write code, but a lot of things are not very convenient, it is recommended to install together.



Qt Assistant (QT Assistant) is a document reader, we can view the QT data we need, and some of the library and function interface.

QT Designer (QT designers) is a powerful cross-platform GUI layout and format builder (and UI-related, as you'll see later)

Qt Demos (QT demo program and examples)

Qt linguist (Qt language home) provides a set of tools to accelerate application translation and internationalization.


Below we start to create our first project

One thing to add: The owner of the project directory path that we created must be a user, not root, or it would fail to create. You can create a project directory under the user directory or create a directory under root and modify it to user owned by Chown owner Dir.

2.1 New GUI Application

Click File--New project--Select QT control project->QT GUI application



Set the project name and the path where the project is located



The note here is that I keep the default state of the line, using the shadow build this tick do not remove, this function is to let us compile the program and generated by the program in two folders, in the first picture can see there are two folders, Hello and hello-build-destop-qt. Hello is stored in the source file, HELLO-BUILD-DESTOP-QT is stored in the generated files, etc.



The class name can be arbitrarily taken, the equivalent of our own class is stored under this class name, base class we choose Qialog. Show us this class of inheritance and Qialog this class, the following are header files and source files, there is an interface file (very important, we explain below)



Here is the project we created and the main.cpp file I wrote myself. Below I will take everybody to analyze this source code together.



The lower left corner has a green triangle for running, or using Qmake-porject main.cpp at the command line will generate a Hello.pro project file, and Qmake will generate makefile files to help me compile these files, and finally make is successful. We will also find that the integrated development of the bad environment is a place, and the command line requires a lot of steps, want to macro processing, connection library, connector files, etc., relatively troublesome, but a relatively thorough understanding of a project, the system more comprehensive understanding.


The following begins to explain the code other header files do not look, we create GUI works automatically generated, we focus on main.cpp and Helloworld.ui these two files.

#include <QApplication>
#include "helloworld.h"
#include <QDialog>
#include <QLabel>
#include <ui_helloworld.h>
#include <QTextCodec>

int main (int argc, char *argv[])
{
    qapplication A (argc, argv);  
    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("UTF-8")); /* Set the encoding to prevent Chinese garbled */
    qdialog W;   /* Create a Qdialog object, which is a dialog box *
/#if 0      
    w.resize (400,300);                /* dialog box size is 400*300 *
    /Qlabel lable (&w);                 /* Create qlable object, parent window is w *
    /Lable.move (10,120);			/* lable The relative position of the label in the dialog box *
    ///lable.settext ("Hello World! Love linux/familiar with python/proficient C ");                  /* Chinese garbled *
    /Lable.settext (QOBJECT::TR ("Hello World! Love linux/familiar with python/proficient C "));       /* Chinese not garbled */
#else
     ui::helloworld Ui;                
     UI.SETUPUI (&w);
#endif
    w.show ();
    return a.exec ();
}

The first few lines are header files, and a header file name represents a class

Qapplication A (argc, argv); Any QT GUI program requires a Qapplication object


Everyone here is going to be very strange. Why do I use an IF-% as the selector switch, because there are two ways to create this project file, if the Walk if 1 section represents a manual write some window display and the label to show Hello World! Love linux/familiar with python/proficient C, if go if 0 part represents is we hand use QT Designer Create interface file.

We click on the Helloworld.ui file under the interface file, then filter to find the label, then hold down the left mouse button to drag it to the main design to go to the interface, double-click it into the edit state input Hello World! Love linux/familiar with python/proficient C, then save, we can call our manual drawing Setup interface program in main.cpp file, need to include # include <ui_helloworld.h>, Because our Helloworld.ui compiled header file is # include <ui_helloworld.h>, the equivalent of the class name you created is Ui::helloworld

We have the same effect on both manual code and hand animation design.

To compile the UI program we can implement it with the command line: Uic-o ui_helloworld.h Helloworld.ui


Create a project to this end, the future of the article I will also from easy to difficult to explain in the form of blog post. Qt is based on C + + development, so we must have a little C + + Foundation, here we recommend the Basic Learning QT books: "Qt Creator QuickStart" and "C + + Primer Chinese version (fourth edition)"

Reference Bo Master: http://blog.csdn.net/goforwardtostep/article/details/52674579#comments


Because Bo Master Knowledge Reserve capacity is limited, if there are errors, but also hope to advise.

@copyright Linux_google

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.