QT线程 Emit、Sgnals、Slot详细解释

前言:

转载请附上连接,本帖原创请勿照抄。

信号和槽机制是 QT 的核心机制,要精通 QT 编程就必须对信号和槽有所了解。信号和槽是一种高级接口,应用于对象之间的通信......

这篇文章给大家讲解最主要的三个点,其它的一大堆内容请参考:该连接 

1、 线程部分

这个线程最主要就是使用了Emit发射信号

.h源文件代码

class Consumer  : public QThread
{
    Q_OBJECT
public:

    void run();

// 信号声明
signals:
    void isDone();  //处理完成信号
};

.cpp实现文件代码

void Consumer::run()
{
    emit isDone();  //发送完成信号
}

2、 界面代码响应

主要实现了接收Emit返回的信号,并绑定槽函数实现响应

.h源文件代码

public:
    void mySlot();//用来响应线程Emit发射的信号
private:
    Consumer *writer; 

.cpp实现文件代码

//构造函数
writer = new Consumer;
connect(writer, &Consumer::isDone, this, &MainWindow::mySlot);

// 定义槽函数 mySlot()
void MainWindow::mySlot()
{
    QMessageBox::about(this,"Tsignal", "响应线程中的mySlot函数");
}

//界面BUT按钮
writer->start();
writer->quit();
writer->wait();

多次点击每次都会接收到信号

3、 断开信号连接

断开后再次点击开启线程不会接收到Emit信息

void MainWindow::on_pushButton_3_clicked()
{

    writer->disconnect(writer,0,0,0);
}

其它线程文章:

QT 初识线程(简单实现):https://blog.csdn.net/qq_37529913/article/details/110127940

QT QMutex使用详解:https://blog.csdn.net/qq_37529913/article/details/110187452

QT 线程之QSemaphore(深入理解):https://blog.csdn.net/qq_37529913/article/details/110187121

QT线程 Emit、Sgnals、Slot详细解释:https://blog.csdn.net/qq_37529913/article/details/110211435

QT 线程之QWaitCondition(深入理解):https://blog.csdn.net/qq_37529913/article/details/110212704

Qt 多线程之线程事件循环(深入理解):https://blog.csdn.net/qq_37529913/article/details/110229382

QT线程之QObjects(深入理解):https://blog.csdn.net/qq_37529913/article/details/110228837

QT线程之可重入与线程安全(深入理解):https://blog.csdn.net/qq_37529913/article/details/110224166

QT之浅拷贝、深拷贝、隐式共享(深度理解必看文章):https://blog.csdn.net/qq_37529913/article/details/110235596

QT 隐式共享机制对STL样式迭代器的影响:https://blog.csdn.net/qq_37529913/article/details/110252454

猜你喜欢

转载自blog.csdn.net/qq_37529913/article/details/110211435