Qt stay C++ Some features are added to the standard , That is to say Qt Own core technology . These core technologies are Qt Core Module . These features mainly include :

* Very powerful seamless connection communication mechanism , They are called signals and slots ;
* Queryable and designable object properties ;
* Hierarchical and queryable object tree ;
* Use the guard pointer (QPointer) The ownership control of objects is realized in a very natural way ;
* Dynamic projection of cross database work ;

One , Meta object system (The Meta-Object System)

Qt The meta object system provides Signal and slot mechanism for communication between objects , Type information and dynamic property system of runtime . The basic structure of meta object system is :

1.QObject class . This class is the base class for all meta object system classes .

2.Q_OBJECT macro . Declare the macro at the beginning of the class's member variable , You can make this class a metaobject , Such as dynamic attributes and signals and slots .

3. Meta Object Compiler (MOC).MOC Tools at compile time , Will contain Q_OBJECT Macro class is interpreted as standard C++ source file , Make the general compiler compile Qt project .

Meta object system is Qt
The core of , We will certainly use it in the development process QObject class ,Q_OBJECT macro , At compile time, the VS The output box of MOC process . however , The meta object system does more than that , It is associated with the following attribute system , Object models and signals are closely related to slots , Or rather , It's the basis of them .

standard C++ The object model provides a very effective support for the running efficiency of the object paradigm . but GUI Programming is both a need to run efficiently , Areas that require high flexibility .Qt Through the object model is very good combination C+++ Speed and flexibility . A lot of them Qt Properties are inherited through QObject class , Using standard C++ Technical implementation . Others such as object communication mechanism and dynamic attribute system , need Qt own MOC To achieve . in short , Meta object system is a C++ extend , bring Qt Language is more suitable for GUI Programming development .

Here are some of the basic classes of design in the meta object model :




Additional information about a class


Meta-data about an enumerator


Meta-data about a member function


Meta-data about a property


Manages named types in the meta-object system


The base class of all Qt objects


Exception-safe wrapper around QObject::blockSignals()


Watches the lifetime of multiple QObjects


Contains meta-information about Qt objects


Template class that provides guarded pointers to QObject


Bundles signals from identifiable senders


Acts like a union for the most common Qt data types



Two , Attribute system (The Property System)

Qt It provides a complex property system , It's like the property system provided by some compiler developers . however , As a class library independent of compiler and development platform ,Qt Do not rely on nonstandard compilation instructions such as __property
or [property].Qt The project can be in any Qt Supported platform standards C++ Compiler compilation . This technology is implemented through the above meta object system .

stay QObject In a derived class of , Using macro Q_PROPERTY() Defining properties , The format is as follows :
Q_PROPERTY(type name              (READ getFunction [WRITE setFunction] |
              MEMBER memberName [(READ getFunction | WRITE setFunction)])
             [RESET resetFunction]              [NOTIFY notifySignal]
             [REVISION int]              [DESIGNABLE bool]             
[SCRIPTABLE bool]              [STORED bool]              [USER bool]
             [CONSTANT]              [FINAL])
Some of the main keyword meanings :

READ: Specifies a function to read property values .

WRITE: Specifies a function that sets the value of an attribute .

MEMBER: Specifies that a member variable is associated with a property .

RESET: Specifies a function that sets the default value of an attribute , Optional .

NOTIFY: Set a signal , Triggered when the property value changes . Optional .

Q_PROPERTY A macro value is defined as a return type Type, The name is name Properties of , use READ and WRITE Keyword defines the read and write functions for properties . It looks more like a member variable of a class . For example :
class MyClass : public QObject { Q_OBJECT Q_PROPERTY(Priority priority READ
priority WRITE setPriority NOTIFY priorityChanged) public: MyClass(QObject
*parent = 0); ~MyClass(); enum Priority { High, Low, VeryHigh, VeryLow };
Q_ENUM(Priority) void setPriority(Priority priority) { m_priority = priority;
emit priorityChanged(priority); } Priority priority() const { return
m_priority; } signals: void priorityChanged(Priority); private: Priority
m_priority; };

Three , Object tree and ownership (Object Trees & Ownership)

When there are multiple associated objects , These objects can organize themselves in the tree . such as , When you create an object of a derived class , It is added to the list of child objects of the parent object , And is released at the same time when the parent object is released . This technology is implemented in the development Qt Project time , You may not need to release you one by one new It came out GUI control , Because they inherited from QObject class , When the parent of the class is deleted and destructed , All of these GUI Control will remove the destructor , No memory leak . Of course, you can also delete these controls manually , When you delete In fact, the child object is deleted from its parent object .

Look at the following example :
int main() { QWidget window; QPushButton quit("Quit", &window); ... }

Review C++ Knowledge of , Analysis of the above code under the deconstruction process . Temporary variables inside functions ( local variable ) Is to allocate controls on the stack , However, the structure is in reverse order ( First in, second out ).quit It's a class QPushButton Object of , The code specifies that its parent object is window. The first step is to decompose quit, At the same time, the object is removed from the window In the list of child objects , Redefining the parent object window. This process is correct . If this is the case :
int main() { QPushButton quit("Quit"); QWidget window;
quit.setParent(&window); ... }

Predissociation windows All of its children are destructed , include quit. When redefining quit Time , It is equivalent to executing the same object twice delete, We know it's here C++ It is not allowed in the standard , Program meeting “ collapse ”.

In development Qt Yes, almost every official class and custom class will have a parent class , Specify its parent. It is very important to understand the concept of object tree and ownership .

Four , Signals and slots (Signals & Slots)

Signals and slots are Qt The core of the core , It is an important feature different from other frameworks . Signal and slot are the communication mechanism between objects , need Qt Support of meta object system .

Signals and slots hide complex underlying implementations , After the correlation between signal and slot is completed , Transmitting signals doesn't need to be known Qt How to find the slot function . This is similar to the callback function of some embedded devices , But compared with the callback function , The signal and slot are slightly slower , But it's much more flexible than callback functions .

The signal is sent out when an event occurs , The slot is a function that responds to this signal . To connect the two connect function .

connect There are many ways to take parameters to a function , One is :

connect(sender, SIGNAL(signal(int)), receiver, SLOT(slot(int)));

In which macro is used SIGNAL and SLOT Specify signal and slot functions , If the signal needs to transfer parameters , Then the parameter type must be specified in the function .

The other way is :

connect(sender, QSender::signal, receiver, QSlot::slot);

For signals and slots with default parameters , That is, the signal name is unique , There are no two signals with the same name but different parameters , This can be used .

Take an example of a custom signal :
class MyClass:public QObject { Q_Object private: int counter = 0; public:
MyClass(QObject *parent = 0){}; ~MyClass(); int add(); signals: void
valChanged(int val); } void MyClass::add() { counter++; emit
valChanged(counter); } //mainwindow.h class QtGuiApplication1 : public
QMainWindow { Q_OBJECT public: QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private: Ui::QtGuiApplication1Class ui; private slots: void onValChanged(int
val); }; //mainwindow.cpp MainWinow::MainWindow (QWidget *parent) :
QMainWindow(parent) { m_class = new MyClass(this); connect(m_calss,
SIGNAL(valChanged(int)), this, SLOT(onValChanged(int))); } void
QtGuiApplication1::onValChanged(int val) { //do sth for val }

©2019-2020 Toolsou All rights reserved,
Conflict statement (conflicting declaration) solve Map---Java judge Map Contains a key Output string at specified position --PTA common 5 species JAVA Runtime exception Unity3D Input Key system PHP Printout log( Essence 2020 year 6 month 3 Daily update ) TypeScript Detailed explanation of Chinese interface Forbes China Auto rich list : He xiaopengdi 11 Li Xiangdi 14 Li Bindi 15( Essence )2020 year 6 month 26 day C# Class library Loop execution help class ( Essence )2020 year 7 month 21 day ASP.NET Core Use of global filters