Getting Started with QT

QT overview

QT is a cross-platform C ++ graphical user interface application framework that is object-oriented .
Focus on:! ! !

  1. Cross-platform
  2. Language: C++
  3. Graphical user interface application framework (my understanding, it is a visualization tool)

Create a QT project

  1. Open QT Creator (I have to say that I have installed QT several times before, and I don’t know what each component is for. Once I open the folder and see a group of icons, I will be stupid), so remind myself again, create a project with It's QT Creator ! ! !
    insert image description here
  2. It looks like this after openinginsert image description here
  3. Create a project
    insert image description here
    Click in the order of 1, 2, and 3 (here, I haven't figured out the types of various projects, I can only remember...) First name
    insert image description here
    the newly created project , then select the path where the project is stored , and finally, Click Next In this step, the compilation package is selected (QT will automatically select it for you). Click Next In this step, the system will add a class inherited from CMainWindow by default, and the name and base class of the class can be modified here . Click Next twice in a row to create a QT program. After the creation is complete, such a page will pop up.
    insert image description here

    insert image description here


    insert image description here

    insert image description here

insert image description here
QMainWindow is the main window class . The main window has a menu bar, toolbar and status bar. It is similar to the main window of a general application.
QWidget is the base class of all classes with visual interfaces . The interface created by selecting QWidget can be used for various interface components. Support
QDialog is a dialog class , which can create a dialog-based interface

Click on the folder you just created, or the folder where you store the project, you can see these files, explain them below
insert image description here
insert image description here

.pro file

.pro is the project file (project), which is a configuration file automatically generated by qmake for producing makefiles.
In general, the file format
insert image description here
template variable of the .pro file: TEMPLATE , this thing tells qmake which kind of makefile to generate for this application
insert image description here

Headers grouping

Under this node are all header files (.h) in the project

Sources grouping

All C++ source files (.cpp) in this project, as shown above, mainwindow.cpp is the implementation file of the main window class, and it is also the entry point of the program

From grouping

Under this node are all interface files (.ui) in the project
. mainwindow.ui is the interface file of the main window.
The interface file is a text file, using XML language to describe the composition of the interface
insert image description here

Run a QT project

Speaking of this, I have to mention my stupid behavior again. Among the piles of generated files, I don’t know which one to run, so I just leave it there and run it randomly...
Emphasis is that the main.cpp file is running! ! !
Open it, you can see the most cost-effective QT project, generate an empty page,
Please add a picture description
click to run, you can see:
insert image description here

Precautions

  1. QT header file does not have .h suffix
  2. A QT class corresponds to a header file, and the class name is the header file name
  3. The QApplication application class
    3.1 manages the control flow and main settings of GUI applications.
    3.2 is the lifeblood of QT's entire background management. It contains the main event loop , in which all events from the window system and other resources are processed and dispatched . It also handles application initialization and termination, and provides dialog management .
    3.3 For any graphical user interface application using QT, there is exactly one QApplication object, regardless of whether the application has 0, 1, 2 or more windows at the same time.
  4. The .exec()
    program enters a message loop, waiting to respond to user input. Here main() transfers control to QT, QT completes the event processing work, and the value of exec() will return when the application is launched. In exec(), QT accepts and processes user and system events and passes them on to the appropriate widgets .

Signals and slots

The signal slot is the observer mode. When a certain event occurs , for example, a button detects that it has been clicked, it will send a signal (signal) , which has no purpose.
If an object is interested in this signal, it will use the connection function (connect) , that is, bind the signal to be processed with one of its own functions (slots) to process this signal .
When the signal is emitted, the connected slot function will be automatically called back.
Look at a chestnut:

#include<QApplication>
#include<QPushButton>

int main(int argc,char *argv[])
{
    
    
    QApplication app(argc,argv);
    QPushButton button("Quit");
    QObject::connect(&button,&QPushButton::clicked,&app,&QApplication::quit);
    button.show();
    return app.exec();
}

Running result:
insert image description here
Click Quit,
connect()the most commonly used general form of the program exit function:

connect(sender,signal,receiver,slot);

parameter:

  1. sender : the object that emitted the signal
  2. signal : the signal sent by the sending object
  3. receiver : the object that receives the signal
  4. slot : the function that the receiving object needs to call after receiving the signal

The signal slot requires that the parameters of the signal and the slot are consistent . The so-called consistency means that the parameter types are consistent. If not, it is permissible that the slot function has fewer parameters than the signal . Even so, the order of the parameters of the slot function must be consistent with the first few of the signal . This is because you can choose to ignore the data from the signal in the slot function (that is, the slot function has fewer parameters than the signal), but it cannot be said that the signal does not have this data at all, you have to use it in the slot function (that is, the slot function The function has more arguments than the signal, which is not allowed).

custom signal slot

The connect() function allows us to connect the signals and slots that come with the system. Qt also allows us to design our own signals and slots.
(Just like C++, it allows us to directly call the built-in library functions of the system, but also allows us to customize functions)
Realize a chestnut of newspapers and subscribers:
there is a newspaper class Newspaper, and a subscriber class Subscriber. Subscriber can subscribe to Newspaper.
In this way, when Newspaper has new content, Subscriber can be notified immediately.

#include <QObject>
// newspaper.h //
class Newspaper : public QObject
{
    
    
 Q_OBJECT
public:
 Newspaper(const QString & name) :
 m_name(name)
 {
    
    
 }
 void send()
 {
    
    
 emit newPaper(m_name);
 }
signals:
 void newPaper(const QString &name);
private:
 QString m_name;
};
/*newspaper类,继承QObject类。只有继承了QObject类的类,才具有信号槽的能力*/

First of all, the newspaper class inherits the QObject class. Only classes that inherit the QObject class have the ability to signal slots

Guess you like

Origin blog.csdn.net/qq_52109814/article/details/121875392