阅读 94

Qt中子进程和父进程之间信号和槽通信

原文链接:

 

testthread.h 文件

#ifndef TESTTHREAD_H
#define TESTTHREAD_H
 
#include 
 
#include "msg.h"
 
class TestThread : public QThread
{
    Q_OBJECT
public:
    explicit TestThread(QObject *parent = 0);
 
protected:
    void run();
 
signals:
    void TestSignal(int);
 
private:
    Msg msg;
};
 
#endif // TESTTHREAD_H

testthread.cpp文件

#include "testthread.h"
 
TestThread::TestThread(QObject *parent) :
    QThread(parent)
{
}
 
void TestThread::run()
{
    //触发信号
    emit TestSignal(123);
}

自己定义的类继承了QThread类,重写run函数,然后触发TestSignal信号。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include 
 
#include "testthread.h"
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private slots:
    void DisplayMsg(int);
 
private:
    Ui::MainWindow *ui;
    TestThread *t;
};
 
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    //进行connect前必须实例化
    t = new TestThread();   
 
    connect(t, SIGNAL(TestSignal(int)), this, SLOT(DisplayMsg(int)));
 
    //执行子线程
    t->start(); 
}
 
void MainWindow::DisplayMsg(int a)
{
    ui->textBrowser->append(QString::number(a));
}
 
MainWindow::~MainWindow()
{
    delete ui;
}

Mainwindow里面连接信号槽,并且将收到的int参数显示在界面上。

 

下面我们对程序进行一些简单,修改,使得它传输我们的自定义消息。

testthread.h 文件

#ifndef TESTTHREAD_H
#define TESTTHREAD_H
 
#include 
 
#include "msg.h"
 
class TestThread : public QThread
{
    Q_OBJECT
public:
    explicit TestThread(QObject *parent = 0);
    Msg msg;
 
protected:
    void run();
 
signals:
    void TestSignal(Msg);   //Msg!!!
};
 
#endif // TESTTHREAD_H

testthread.h 文件

#include "testthread.h"
 
TestThread::TestThread(QObject *parent) :
    QThread(parent)
{
}
 
void TestThread::run()
{
    msg.int_info = 999;
    msg.str_info = "Hello Main Thread!";
    //触发信号
    emit TestSignal(msg);
}

mainwindow.h 文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include 
 
#include "testthread.h"
#include "msg.h"
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private slots:
    void DisplayMsg(Msg);   //Msg!!!
 
private:
    Ui::MainWindow *ui;
    TestThread *t;
};
 
#endif // MAINWINDOW_H

mainwindow.cpp 文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 
    //进行connect前必须实例化
    t = new TestThread();
 
    //Msg!!!
    connect(t, SIGNAL(TestSignal(Msg)), this, SLOT(DisplayMsg(Msg)));
 
    //执行子线程
    t->start();
}
 
void MainWindow::DisplayMsg(Msg msg)
{
    ui->textBrowser->append(QString::number(msg.int_info));
    ui->textBrowser->append(msg.str_info);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}

此时再进行编译,能够通过,但是Qt Creator会有提示

QObject::connect: Cannot queue arguments of type Msg
(Make sure Msg is registered using qRegisterMetaType().)

并且运行程序,不会有任何反应。

 

mainwindow.cpp文件 改动为

ui->setupUi(this);
 
qRegisterMetaType("Msg");

可以正常运行

 

说明:

在线程间使用信号槽进行通信时,需要注意必须使用元数据类型

Qt内生的元数据类型,如int double QString 等

如果要用自己定义的数据类型,需要在connect前将其注册为元数据类型。形式见代码。

 

代码中MSG相当于自己定义的一个类或者一个struct,主要关注父子进程间connect就可以

原文:https://www.cnblogs.com/kongbursi-2292702937/p/15090010.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐