QT learning notes-01, learning notes-01
This is my note for studying Qt, and it should not be too careful.
The first thing is HelloWorld,
So, create an Apllication program and select Qt Widgets Application. (In my tutorial, select Qt Gui application, and create the QT desktop program)
1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <qlabel.h> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 //MainWindow w; 9 //w.show();10 QLabel la("Hello world");11 la.show();12 13 return a.exec();14 }
After clicking the Green Arrow, the interface is displayed. Is it a success ?!
//---------------------------------------------------------------
Signal slot
The so-called signal slot is actually the observer mode. When an event occurs, for example, when a button detects that it has been clicked, it sends a signal ). Such sending has no purpose, similar to broadcasting. If an object is interested in this signal, it will use the connect function, which means to process the signal with its own function (becoming a slot. That is, when the signal is sent, the connected slot function is automatically called back. This is similar to the observer mode: when an event of interest occurs, an operation is automatically triggered.
The above sentence is the content in the document, which is easy to understand and cannot be described by yourself.
The button Qt is calledQPushButton
1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <qlabel.h> 4 #include <qpushbutton.h> 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication a(argc, argv); 9 //MainWindow w;10 //w.show();11 QLabel la("Hello world");12 la.show();13 QPushButton button("Quit");14 QObject::connect(&button, &QPushButton::clicked, &QApplication::quit);15 button.show();16 17 return a.exec();18 }
View Code
The result is not a Helloworld as I expected, followed by a button, but only one button. Well, you need to figure out what is going on later.
QObject::connect()
There are five reloads:
123456789101112131415161718 |
QMetaObject: Connection connect (const QObject *, const char *, const QObject *, const char *, Qt: ConnectionType); QMetaObject: Connection connect (const QObject *, const QMetaMethod &, const QObject *, const QMetaMethod &, Qt: ConnectionType); QMetaObject: Connection connect (const QObject *, const char *, const char *, Qt :: connectionType) const; QMetaObject: Connection connect (const QObject *, PointerToMemberFunction, const QObject *, PointerToMemberFunction, Qt: ConnectionType) QMetaObject: Connection connect (const QObject *, PointerToMemberFunction, functor ); |
First, the sender type isconst QObject *
The signal type isconst char *
, The explorer type isconst QObject *
The slot type isconst char *
. This function processes signal and slot as strings.
Second, sender and consumer er areconst QObject *
But both signal and slot areconst QMetaMethod &
. We can think of every functionQMetaMethod
. Therefore, this method can be usedQMetaMethod
Type comparison.
Third, sender is alsoconst QObject *
, Signal and slot are the sameconst char *
But it lacks the aggreger. This function uses the this pointer as the handler.
Fourth, both sender and consumer er exist.const QObject *
But the signal and slot types arePointerToMemberFunction
. You should know the name. This is a pointer to the member function.
Fifth, there is no difference between the first two parameters. The last parameter isFunctor
Type. This type can be static functions, global functions, and Lambda expressions.
The above section is also excerpted, because it is still understandable in a short period of time and cannot be used in your own words.
Any class that inherits QObject has the signal slot function. All QObject classes (whether directly or indirectly) should be written in the first line of code.Q_OBJECT
.
The macro expansion will provide the signal slot mechanism, internationalization mechanism, and C ++ RTTI-based reflection capability provided by Qt for our class.
One plusQ_OBJECT
An error occurs because the macro is not noticed and should be placed in the header file. If the class is put in the cpp file, there will be an error and there is no way to perform moc. Put the class in the header file.
Precautions for custom signal slots:
- Both the sender and receiver must be
QObject
(Of course, the slot function is a global function, Lambda expression, and so on without the recipient );
- Use signals to mark the signal function. The signal is a function declaration that returns void without implementing function code;
- A slot function is a common member function. As a member function, it is affected by public, private, and protected;
- Use emit to send signals at appropriate locations;
- Use
QObject::connect()
Function connection signals and slots.