Qt中的线程与信号槽

本文介绍了如何在Qt中使用ZThread类创建一个基础的多线程应用程序,展示了如何正确连接信号与槽,以及在C++中处理信号的注意事项。
摘要由CSDN通过智能技术生成

小实践

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
// #pragma once
#include <QMainWindow>
#include"zthread.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class ZThread;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void onEmitSignalUpdate(int num);

private:
    Ui::MainWindow *ui;
    ZThread* m_pThtread;
};
#endif // MAINWINDOW_H

zthread.h

// #pragma once
#ifndef ZTHREAD_H
#define ZTHREAD_H
#include<QObject>
#include<QThread>
#include<QDebug>

// QObject
class ZThread : public QThread
{
    Q_OBJECT
public:
    explicit ZThread(QObject *parent = nullptr);
    ~ZThread();

    void stopthreadrunning();

signals:
    void updateThreadsignal(int num);

protected:
    void run();

private:
    bool m_bRunning; //控制多线程运行。b是布尔类型
    int m_ncount; //n是number类型
};

#endif // ZTHREAD_H

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
// #include "zthread.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_pThtread=new ZThread();
    m_pThtread->start();
    connect(m_pThtread,&ZThread::updateThreadsignal,this,&MainWindow::onEmitSignalUpdate);
    // connect(m_pThtread,SIGNAL(updateThreadsignal(int)),this,SLOT(onEmitsignalupdate(int))); //这样的写法也可以
}

MainWindow::~MainWindow()
{
    if(m_pThtread!=nullptr)
    {
        m_pThtread->stopthreadrunning();
        m_pThtread->quit();
        m_pThtread->wait();
        m_pThtread->deleteLater();
    }

    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    m_pThtread->stopthreadrunning();
}



void MainWindow::onEmitSignalUpdate(int num)
{
    qDebug()<<"==========Thread Signal Update"<<num<<Qt::endl;
}

zthread.cpp

#include "zthread.h"

ZThread::ZThread(QObject *parent)
    : QThread{parent},m_bRunning(true),m_ncount(0)
{

}

ZThread::~ZThread()
{

}

void ZThread::run() //The threaded code runs in this
{
    while(m_bRunning) //The code that all threads run on is written here
    {

        // qDebug()<<"T is running......"<<Qt::endl;
        ++m_ncount;

        emit updateThreadsignal(m_ncount);
         msleep(1000); //Milliseconds
    }
    qDebug()<<"=====T is exit====="<<Qt::endl;
}

void ZThread::stopthreadrunning()
{
    m_bRunning=false;
}

界面:

运行结果:

心得

我好像明白了点,我确实语法错了。之前关联信号与槽的时候,明明是不同的两个类,我却在connect前边加上了一个类域,这是其一;信号函数没有函数体定义,不能在.cpp文件中实现,但是我却实现了,还加了类域

没想到  重定义  三个字放到信号signals这里是这样的情况,我粗略地以为只是函数、变量对象等的“重定义、”

 connect(m_pThtread,&ZThread::updateThreadsignal,this,&MainWindow::onEmitSignalUpdate);  函数指针的写法也可以了

注意不要犯错:

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒回顾,半缘君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值