Blocking parameters for Qt signal connections

Qt signal blocking:

  Calling a function through the signal-slot mechanism

  The application scenario is that the sub-thread actively informs the main thread of some information under multi-threading and waits for the main thread to process the information

Let's look at the code implementation:

  mythread.h/.cpp

//mythread.h :
class MyThread : public QThread
{
    Q_OBJECT
public:
    MyThread();
    ~MyThread();

signals:
     void slgTest(int iCount);

private:
    QTimer* _pTimer;
    void run();

private slots:
    void sltTimer();
};

//mythread.cpp
MyThread::MyThread():
    _pTimer(nullptr)
{
}

MyThread::~MyThread()
{
    if(nullptr != _pTimer){
        _pTimer->stop();
        delete _pTimer;
    }
    this->quit();
    this->wait();
}

void MyThread::run()
{
    _pTimer = new QTimer;
    connect(_pTimer,&QTimer::timeout,this,&MyThread::sltTimer,Qt::DirectConnection);
    _pTimer->start(500);
    this->exec();
}

void MyThread::sltTimer()
{
    static int s_iCount = 0;
    s_iCount++;
    qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<"  begin";
    emit slgTest(s_iCount);
    qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<"  end";
}

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

  mainwindow.h/.cpp

//mainwindow.h
class mainWindow : public QWidget
{
    Q_OBJECT

public:
    explicit mainWindow(QWidget *parent = 0);
    ~mainWindow();

private:
    Ui::mainWindow *ui;
    MyThread _myThread;

private slots:
    void sltReveice(int iCount);
};

//mainwindow.cpp
mainWindow::mainWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainWindow)
{
    ui->setupUi(this);
    connect(&_myThread,SIGNAL(slgTest(int)),this,SLOT(sltReveice(int)));
    _myThread.start();
}

mainWindow::~mainWindow()
{
    delete ui;
}


void mainWindow::sltReveice(int iCount)
{
    qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<"  begin";
    QTime current_time =QTime::currentTime();
    qDebug()<<"time:"<<"minute:"<<current_time.minute()<<"second:"<<current_time.second()<<"msec:"<<current_time.msec();
    qDebug()<<"received data:"<<iCount;
    qDebug()<<"Thread ID:"<<QThread::currentThreadId()<<"  end";
}

  PS: Here is a record of the code
    1. The construction of QTimer must be inside the run() function to realize the construction of the slot function triggered by the timer in the thread, and then perform the message loop.

    2. The signal slot chain connection must be written as: connect(_pTimer,&QTimer::timeout,this,&MyThread::sltTimer);

    If it is written as connect(_pTimer,SIGNAL(*),this,SLOT(*)); the form will prompt that the slot function cannot be linked, the reason should be that the run function rewrites the parent class QThread, and this in run should point to QThread , instead of mythread.

    3. The timer signal slot link parameter in the thread must be Qt::DirectConnection

    4. There must be Q_OBJECT, and the macro definition Q_OBJECT is a mandatory requirement for any implementation of signals, slots or attributes.

The result of running the above code:

 

As shown in the figure above, there is no problem with the data interaction between the two threads, but what if I sleep in the main thread (simulating time-consuming functions)

Add usleep(1000) in sltReveice(); (sleep header file under linux: #include <unistd.h>)

operation result:

 

As shown in the figure above, it is seriously out of sync

PS: It will be triggered continuously here, and the signal will be queued continuously (this kind of queue should only exist in the context of multi-threaded applications)

Now set to block connect(&_myThread, SIGNAL(slgTest(int)), this, SLOT(sltReveice(int)), Qt::BlockingQueuedConnection);

operation result:

 

As shown in the figure above, the sub-threads are now neatly arranged, and the sub-threads are waiting for the main thread to complete processing. Going back to the beginning of "calling functions through the signal-slot mechanism", that is to say, the sub-threads call a function.

Off-topic: The above mentioned the situation where the signals sent by the thread are continuously queued.

The program is slightly changed in the above program
1. Add a private variable to mythread and increase it every time the sub-thread timer arrives

2. The private variable of mythread turns off the timer when it reaches 20

3. Add a method to obtain private variable values ​​for mythread

code no longer shown here

The result of running the code is:

 

As shown in the figure above, the timer has ended, but the slot function of the main thread is still being triggered

The processing method here is to disconnect when entering the slot function, and reconnect when exiting the function (the program code and running results will no longer be displayed)

Disconnect function: disconnect

How to disconnect from PS:

1. A large number of disconnected signal and slot functions
  Disconnect all signal slots connected to an object: disconnect(myObject, 0, 0, 0); myObject->disconnect();

  Disconnect all signal slots connected to a signal: disconnect(myObject, SIGNAL(mySignal()), 0, 0); myObject->disconnect(SIGNAL(mySignal()));

  Disconnect all signal-slot relationships between two objects: disconnect(myObject, 0, myReceiver, 0);myObject->disconnect(myReceiver);

2. The specified signal is simply disconnected

  QMetaObject::Connection dis = connect(....); disconnect(dis);

  sender()->disconnect();

The benefits of this article, free to receive Qt development learning materials package, technical video, including (Qt actual combat project, C++ language foundation, C++ design mode, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project 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/132208331