Qt 信号槽返回值

22 篇文章 2 订阅

1、定义线程间交互数据

#ifndef MESSAGE_H
#define MESSAGE_H
#include <QMetaType>
#include <QVariant>

struct Message{
    QVariant data;
};
Q_DECLARE_METATYPE(Message)
#endif // MESSAGE_H

2、 业务类

#ifndef BUSINESS_H
#define BUSINESS_H
#include <QObject>
#include "Message.h"
class Business : public QObject
{
    Q_OBJECT
public:
    explicit Business(QObject *parent = nullptr);
public slots:
	// 带有返回值的槽函数
    int onAdd();
    Message onSub(Message in);
};
#endif // BUSINESS_H
#include "business.h"
#include <QTimer>
#include <QDebug>
#include <QThread>
Business::Business(QObject *parent) : QObject(parent)
{
    QTimer *t = new QTimer(this);
    connect(t,&QTimer::timeout,this,[](){
       qDebug()<<"timer:"<<QThread::currentThread();
    });
    t->start(10000);
}
int Business::onAdd()
{
    static int i = 0;
    qDebug()<<Q_FUNC_INFO<<QThread::currentThread();
    return  i++;
}

Message Business::onSub(Message in)
{
    qDebug()<<Q_FUNC_INFO<<QThread::currentThread();
    Message ret ;
    ret.data = QVariant::fromValue(in.data.toInt()-1);
    return  ret ;
}

3、GUI线程

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    //add btn
    void on_pushButton_clicked();
    // sub btn
    void on_pushButton_2_clicked();
signals:
	// 定义带返回值的信号
    int getVal();
private:
    QObject * b ;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "business.h"
#include <QThread>
#include <QDebug>
#include "Message.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    b = new Business;
    //直接连接
    connect(this,SIGNAL(getVal()),b,SLOT(onAdd()),Qt::DirectConnection);
//    connect(this,SIGNAL(getVal()),b,SLOT(onAdd()),Qt::QueuedConnection);
//    connect(this,SIGNAL(getVal()),b,SLOT(onAdd()),Qt::AutoConnection);
//    connect(this,SIGNAL(getVal()),b,SLOT(onAdd()),Qt::UniqueConnection);
//    connect(this,SIGNAL(getVal()),b,SLOT(onAdd()),Qt::BlockingQueuedConnection);
    QThread *work = new QThread;
    b->moveToThread(work);
    work->start();

}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    qDebug()<< thread();
    qDebug()<<"add:"<<getVal();
}

void MainWindow::on_pushButton_2_clicked()
{
    static int t = 10000;
    Message in;
    in.data = QVariant::fromValue(t);
    Message ret;
    qDebug()<< b->thread();
    qDebug()<< thread();
    //这里要防止阻赛
    if(b->thread() != thread())
    {
        QMetaObject::invokeMethod(b, "onSub", Qt::BlockingQueuedConnection,
                                  Q_RETURN_ARG(Message,ret ),
                                  Q_ARG(Message, in));

        qDebug() <<"sub:"<<ret.data.toInt();
        t = ret.data.toInt();
    }
}

4、效果

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值