QT 多线程信号与槽(一)

1.一个主界面(主线程),一个次线程,次线程通过信号向主线程传递递增变量,主线程将它显示出来

mythread.h

#include <QThread>
#include <QDebug>
class Mythread : public QThread
{
    Q_OBJECT
public:
    Mythread();
protected:
    void run();
private:
    int i;
signals:
    void signal_add_int(QString);
};
mythread.cpp

#include "mythread.h"
#include <QDebug>
Mythread::Mythread()
{
    i = 0;
}
 
void Mythread::run()
{
    while(1)
    {
        i++;
        QString str = QString::number(i,10);
        emit signal_add_int(str);
        sleep(1);
        qDebug()<<"run thread:" <<QThread::currentThreadId();
    }
}
mainwindow.h
#include <QMainWindow>
#include "mythread.h"
namespace Ui {
    class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
    Mythread thread;
private slots:
    void set_lineEdit_text(QString);
    void start_thread();
};
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&thread,SIGNAL(signal_add_int(QString)),this,SLOT(set_lineEdit_text(QString)),Qt::QueuedConnection);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(start_thread()));
    qDebug()<<"MainWindow:" <<QThread::currentThreadId();
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::set_lineEdit_text(QString str)
{
    ui->lineEdit->setText(str);
    qDebug()<<"from thread slot:" <<QThread::currentThreadId();
}
void MainWindow::start_thread()
{
    thread.start();
}
打印结果:
MainWindow: 3062654672 
from thread slot: 3062654672 
run thread: 3017550736 
from thread slot: 3062654672 
run thread: 3017550736 
from thread slot: 3062654672 
run thread: 3017550736 
from thread slot: 3062654672 
结论:
     主线程运行thread.start()后,次线程启动,进入到次线程中,
     次线程发送信号给主线程,主线程接收到该信号,进入槽函数,该槽函数也在主线程中运行。
     次线程的开始和结束都在run函数中开始和结束




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Qt中,信号是一种用于对象间通信的机制,可以在多线程环境下使用。通过信号,一个对象可以发射信号,而其他对象可以连接到这个信号并执行相应的函数。 在多线程中使用信号时,需要注意以下几点: 1. 当信号的发送与对象的函数的执行在不同线程中时,可能会产生临界资源的竞争问题。因此,需要使用线程间同步机制来保护共享资源的访问。 2. 在Qt中,QThread继承自QObject,因此可以使用发射信号和定义函数的能力。QThread默认声明了几个关键信号: - started():线程开始运行时发射的信号。 - finished():线程完成运行时发射的信号。 - terminated():线程被异常终止时发射的信号。 下面是一个示例代码,演示了在Qt中如何使用信号进行多线程通信: ```cpp #include <QThread> #include <QDebug> // 自定义线程类 class MyThread : public QThread { Q_OBJECT public: void run() override { qDebug() << "Thread started"; // 执行一些耗时操作 // ... // 发射信号 emit mySignal("Hello from thread"); qDebug() << "Thread finished"; } signals: void mySignal(const QString& message); }; // 自定义函数 class MyObject : public QObject { Q_OBJECT public slots: void mySlot(const QString& message) { qDebug() << "Received message:" << message; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MyThread thread; MyObject object; // 连接信号 QObject::connect(&thread, SIGNAL(mySignal(QString)), &object, SLOT(mySlot(QString))); // 启动线程 thread.start(); return a.exec(); } ``` 这段代码中,我们创建了一个自定义的线程类`MyThread`,其中重写了`run()`函数,在函数中执行一些耗时操作,并发射了一个自定义的信号`mySignal`。然后,我们创建了一个自定义的对象`MyObject`,其中定义了一个函数`mySlot`,用于接收信号并处理。在`main()`函数中,我们创建了线程对象和对象对象,并使用`QObject::connect()`函数将信号连接起来。最后,启动线程并运行Qt事件循环。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值