第八十一课、信号与槽的连接方式------------------狄泰软件学院

一、深入信号与槽的连接方式

1、深入信号与槽的连接方式

(1)、Qt::DirectConnection : 立即调用

(2)、Qt::QueuedConnection : 异步调用

(3)、Qt::BlockingQueuedConnection : 同步调用

(4)、Qt::AutoConnection : 默认连接

(5)、Qt::UniqueConnection : 单一连接

2、小知识

(1)、信号与槽的连接方式决定槽函数调用时候的相关行为

3、知识回顾

(1)、每一个线程都有自己的事件队列

(2)、线程通过事件队列接收信号

(3)、信号在事件队列里被处理

二、各种连接方式详述

MyThread对象的run函数发射对象给MyObject的槽

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <QObject>

class MyObject : public QObject
{
    Q_OBJECT
public:
    explicit MyObject(QObject *parent = 0);
    
signals:
    
protected slots:
    void testSlot();
};

#endif // MYOBJECT_H
MyObject.h
#include "MyObject.h"
#include <QDebug>
#include <QThread>

MyObject::MyObject(QObject *parent) :
    QObject(parent)
{
}
void MyObject::testSlot()
{
    qDebug() << "void MyObject::testSlot() tid=" << QThread::currentThreadId();
}
MyObject.cpp
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);
    
signals:
    void testSignal();
private:
    void run();
};

#endif // MYTHREAD_H
MyThread.h
#include "MyThread.h"
#include <QDebug>

MyThread::MyThread(QObject *parent) :
    QThread(parent)
{
}

void MyThread::run()
{
    qDebug() << "void MyThread::run() tid=" << QThread::currentThreadId();

    for(int i=0; i<2; i++)
    {
        qDebug() << "void MyThread::run() i=" << i;

        sleep(1);
    }
    emit testSignal();

    qDebug() << "void MyThread::run() end";

}
MyThread.cpp
#include <QtCore/QCoreApplication>
#include "MyObject.h"
#include "MyThread.h"
#include <QDebug>

void Direct_connect()
{
    static MyObject m;//m和t都是依附于主函数
    static MyThread t;
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::DirectConnection);

    t.start();
    t.wait(5*1000);
    t.quit();
}
void Queued_connect()
{
    static MyObject m;//m和t都是依附于主函数
    static MyThread t;
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::QueuedConnection);

    t.start();
    t.wait(5*1000);
    t.quit();
}
void BlockingQueued_connect()
{
    static MyObject m;//m和t都是依附于主函数
    static MyThread t;
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::BlockingQueuedConnection);

    t.start();
    t.wait(5*1000);
    t.quit();
}
void Auto_connect()
{
    static MyObject m;//m和t都是依附于主函数
    static MyThread t;
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::AutoConnection);

    t.start();
    t.wait(5*1000);
    t.quit();
}
void Unique_connect()
{
    static MyObject m;//m和t都是依附于主函数
    static MyThread t;
    //连接两次只调用一次
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::UniqueConnection);
    QObject::connect(&t, SIGNAL(testSignal()),&m, SLOT(testSlot()), Qt::UniqueConnection);

    t.start();
    t.wait(5*1000);
    t.quit();
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    qDebug() << "int main(int argc, char *argv[]) tid=" << QThread::currentThreadId();

    //Direct_connect();
    //Queued_connect();
    //BlockingQueued_connect();
   // Auto_connect();
    Unique_connect();


    return a.exec();
}
main.cpp

1、Qt::DirectConnection : 立即调用

(1)、直接在发送信号的线程中调用槽函数,等价于槽函数的实时调用

//打印结果 
int
main(int argc, char *argv[]) tid= 0x1abc void MyThread::run() tid= 0x191c void MyThread::run() i= 0 void MyThread::run() i= 1 void MyObject::testSlot() tid= 0x191c//槽函数在发射的线程中被调用,且比void MyThread::run() end先打印,说明是相当于之间调用 void MyThread::run() end

2、Qt::QueuedConnection : 异步调用

(1)、信号发送到目标线程(目标线程由对象依赖性决定)的事件队列中,由目标线程处理,当前线程继续往下执行

//打印结果
int
main(int argc, char *argv[]) tid= 0x1aec void MyThread::run() tid= 0x1b5c void MyThread::run() i= 0 void MyThread::run() i= 1 void MyThread::run() end void MyObject::testSlot() tid= 0x1aec//在所依附的线程(主线程中被调用),且比void MyThread::run() end后打印,说明发射信号后线程继续往下执行,为异步调用

3、Qt::BlockingQueuedConnection : 同步调用

(1)、信号发送到目标线程的事件队列中,由目标线程处理;当前线程等待槽函数返回,之后继续往下执行

(2)、注意:目标线程和当前线程必须不同

//打印结果
int
main(int argc, char *argv[]) tid= 0x18c4 void MyThread::run() tid= 0x1bf8 void MyThread::run() i= 0 void MyThread::run() i= 1 void MyObject::testSlot() tid= 0x18c4//在void MyThread::run() end前打印,说明得等槽函数返回才继续往下执行 void MyThread::run() end

4、Qt::AutoConnection : 默认连接

(1)、AutoConnection 是connect函数第五个参数的默认值,也是工程中最常用的连接方式

 

int main(int argc, char *argv[]) tid= 0x1988
void MyThread::run() tid= 0x1970
void MyThread::run() i= 0
void MyThread::run() i= 1
void MyThread::run() end
void MyObject::testSlot() tid= 0x1988

5、Qt::UniqueConnection : 单一连接

(1)、描述:

A、功能与AutoConnection相同,自动确定连接类型

B、同一个信号同一个槽函数只有连接一次

 (2)、小知识

A、默认情况下,同一个信号可以多次连接到同一个槽函数

B、多次连接意味着同一个槽函数的多次调用

int main(int argc, char *argv[]) tid= 0x37c
void MyThread::run() tid= 0x1b70
void MyThread::run() i= 0
void MyThread::run() i= 1
void MyThread::run() end
void MyObject::testSlot() tid= 0x37c//只调用一次

三、小结

(1)、信号与槽的连接存在多种方式

 (2)、立即调用方式等价于槽函数的实时调用

(3)、默认方式自动确定连接类型

(4)、同步方式中目标线程与当前线程必须不同

(5)、单一连接方式确保同一个信号与同一个槽函数之间只有一个连接

原文地址:https://www.cnblogs.com/gui-lin/p/6501334.html