Qt两个进程,一个进程发送数据不受控制,一个进程处理需要时间,使用队列进行处理

#include <QObject>
#include <QTimer>
#include<QDebug>
class DataSender : public QObject {
    Q_OBJECT
public:
    DataSender(QObject *parent = nullptr) : QObject(parent)
    {
        connect(&timer, &QTimer::timeout, this, &DataSender::sendData);
        timer.start(1); // 每100ms发送一次数据
    }

signals:
    void dataReady(const QString &data);

private slots:
    void sendData() {
        static int count = 0;
        emit dataReady(QString("Data %1").arg(++count));
        qDebug()<<"Send Data:"<<count;
        if(count==6)
        {
            timer.stop();
        }
    }

private:
    QTimer timer;
};

#include <QObject>
#include <QQueue>
#include <QDebug>
#include<QTimer>
#include<QThread>
class DataReceiver : public QObject {
    Q_OBJECT
public:
    DataReceiver(QObject *parent = nullptr) : QObject(parent) {
        // 初始化队列
        connect(&timer,&QTimer::timeout,this,&DataReceiver::processData);
        timer.start(50);

    }

public slots:
    void receiveData(const QString &data) {
        dataQueue.enqueue(data);
        qDebug()<<"dataQueue: "<<dataQueue.size();

   //     processData();
    }

    void processData() {
        qDebug()<<"**************idle***************";
        if (!dataQueue.isEmpty() && !isProcessing) {
            isProcessing = true;
            QString dataToProcess = dataQueue.dequeue();

            // 模拟数据处理时间
               QThread::msleep(2000);
                qDebug() << "Processed:" << dataToProcess;
                isProcessing = false;
                processData(); // 处理完一个数据后,检查队列中是否还有数据需要处理

        }
    }

private:
    QQueue<QString> dataQueue;
    bool isProcessing = false;
    QTimer  timer;
};

#include <QObject>
#include <QQueue>
#include <QDebug>
#include<QTimer>
#include<QThread>
class DataReceiver : public QObject {
    Q_OBJECT
public:
    DataReceiver(QObject *parent = nullptr) : QObject(parent) {
        // 初始化队列
    }

public slots:
    void receiveData(const QString &data) {
        dataQueue.enqueue(data);
        qDebug()<<"dataQueue: "<<dataQueue.size();

        processData();
    }

    void processData() {
        qDebug()<<"**************idle***************";
        if (!dataQueue.isEmpty() && !isProcessing) {
            isProcessing = true;
            QString dataToProcess = dataQueue.dequeue();

            // 模拟数据处理时间
               QThread::msleep(3000);
                qDebug() << "Processed:" << dataToProcess;
                isProcessing = false;
                processData(); // 处理完一个数据后,检查队列中是否还有数据需要处理

        }
    }

private:
    QQueue<QString> dataQueue;
    bool isProcessing = false;
};

#include <QCoreApplication>
#include "datasender.h"
#include "datarecieve.h"

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    DataSender sender;
    DataReceiver receiver;

    QObject::connect(&sender, &DataSender::dataReady, &receiver, &DataReceiver::receiveData,Qt::QueuedConnection);

    return a.exec();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值