What are the connection methods of Qt signals and slots

Introduction

Signals and slots are one of the proud mechanisms of the Qt framework. When the user triggers an event, a signal (signal) is sent out, which has no purpose, similar to broadcasting. If an object is interested in this signal, it will connect (connect) and bind a function (called a slot) to handle this signal. That is to say, when the signal is sent, the connected slot function will be called back automatically. This is a bit similar to the observer mode in the development mode, that is, when an event of interest occurs, an operation will be automatically triggered

Signals and slots are Qt's unique information transmission mechanism and an important basis for Qt design programs. It allows objects that do not interfere with each other to establish a connection. The essence of a slot is a member function of a class, and its parameters can be of any type. There is almost no difference from ordinary C++ member functions. It can be a virtual function or be overloaded. It can be public, protected, private, or called by other C++ member functions. The only difference is: the slot can be connected to the signal, and whenever the signal connected to the slot is emitted, the slot will be called

The fifth parameter of the connection signal slot connect function

connect 函数原型如下:
[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

ConnectionType is an enumeration defined in Qt namespace, the specific content is as follows:

enum ConnectionType {
 AutoConnection,
 DirectConnection,
 QueuedConnection,
 BlockingQueuedConnection,
 UniqueConnection =  0x80
};
  • Qt::AutoConnection: the default value. Make a judgment when the signal is sent according to the threads where the sender and receiver are located. If in the same thread, use Qt::DirectConnection to connect, otherwise use Qt::QueuedConnection to connect. It should be noted that this judgment has nothing to do with the thread where the sender object is located. What is really judged is the thread where the action of sending the signal is located.
  • Qt::DirectConnection: The slot function will be called directly when the signal is sent, and the slot function runs in the thread where the signal sender is located. The effect looks as if the slot function was called directly at the point where the signal was sent. It should be noted that it is more dangerous in a multi-threaded environment and may cause a crash
  • Qt::QueuedConnection: The slot function is called when control returns to the event loop of the thread where the receiver is located, and the slot function runs on the thread where the signal receiver is located. After the signal is sent, the slot function will not be called immediately. The slot function will not be called until the receiver's current function is executed and the event loop is entered. This is generally used in a multi-threaded environment
  • Qt::BlockingQueuedConnection: The calling timing of the slot function is the same as that of Qt::QueuedConnection, but the thread where the sender is located will be blocked after sending the signal until the slot function finishes running. This may be required when synchronization between multiple threads is required. It should be noted that the receiver and sender must not be in the same thread, otherwise the program will deadlock
  • Qt::UniqueConnection: This flag can be used in combination with the above four by bitwise or (|). When this flag is set, when a signal and slot are already connected, repeated connections will fail, that is, repeated connections are avoided

How signals and slots are connected

C++ connect signal slot - Qt4 syntax

connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(close()));

C++ connect signal slot - Qt5 syntax

connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::close)

C++ connection signal slot - function pointer

void(MainWindow:: *buttonClickSlot)() = &MainWindow::onButtonPushed;
connect(ui->pushButton, &QPushButton::clicked, this, buttonClickSlot);

C++ Connect Signal-Slot - Lambda Expression

connect(ui->pushButton, &QPushButton::clicked, this, [=](){ this->close(); });

C++ signals connected to QML slots

class Test {
signals:
 void sendData(QString str);    
}

1) If you are registering a global object, you need to use Connections to connect:

Connections {
    target: test
    onSendData: {
        console.log(str)
    }
}

2) If the registration is a class, you need to instantiate the object first, and then directly use on to receive:

Test {
 onSendData: {
        console.log(str)
    }
}

QML signals connect to C++ slots

#include <QQuickItem>
QObject *quitButton = root->findChild<QObject*>("quitButton");
if (quitButton) {
    QObject::connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()));
}

C++ calls QML functions

QObject *changeBtn = root->findChild<QObject*>("objectName");
if (changeBtn)
{
    QMetaObject::invokeMethod(changeBtn, "changeColor");
}

QML calls C++ functions

onClicked:
{
    className.test();
}

QML signals connect to QML slots

// A.qml
Rectangle {
 signal sendData(var data)
}
 
// B.qml
Rectangle {
 onSendData: console.log(data)
}

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/130566398