继承自qthread类不适用于排队连接

时间:2019-10-29 10:06:38

标签: qt c++11

我为生产者消费者方法编写了简单的智能容器类。容器中的数据通过posix线程通过callBack添加,之后 线程正在唤醒并解析所有收到的消息。主要的问题是 我的其他QObject子类的线程调用方法都存在信号发出,在信号发出后,插槽仅接收直接连接的主要问题是正常工作。

   #ifndef SHAREDCONTAINEREXTERNAL_H
#define SHAREDCONTAINEREXTERNAL_H
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QQueue>
#include <QObject>

class Device;

class SharedContainerExternal : public QThread
{
    Q_OBJECT
public:
    explicit SharedContainerExternal(QObject* parent = nullptr);
    ~SharedContainerExternal()override;
    void setRawMessage(const QJsonObject& _message);
    void setConsumerObject(Device* _consumer);
    void consumerRead();

protected:
    void run() override;

private:
    QMutex mtx;
    QWaitCondition cv;
    QQueue<QJsonObject> messages;
    Device* consumer;
    bool canAccomplish;
};


#include "sharedcontainerexternal.h"
#include "device.h"

SharedContainerExternal::SharedContainerExternal(QObject* parent) : QThread (parent)
{
    canAccomplish = false;
}

SharedContainerExternal::~SharedContainerExternal()
{
    mtx.lock();
    canAccomplish = true;
    cv.notify_all();
    mtx.unlock();
    wait();
}

void SharedContainerExternal::setRawMessage(const QJsonObject &_message)
{
    mtx.lock();
    messages.push_back(_message);
    cv.wakeAll();
    mtx.unlock();
}

void SharedContainerExternal::run()
{
    while(!canAccomplish){
        mtx.lock();
        if(messages.isEmpty() && !canAccomplish)
            cv.wait(&mtx);
        consumerRead();
        mtx.unlock();
    }
}

void SharedContainerExternal::setConsumerObject(Device *_consumer)
{
    consumer = _consumer;
}

void SharedContainerExternal::consumerRead()
{
    while(!messages.isEmpty()){
        consumer->getDriverMessage(messages.front());
        messages.pop_front();
    }
}

所以在驱动程序类中,我发出了一个信号。

void Device::getDriverMessage(const QJsonObject& _message)
{
    emit sendCallBackMessage(_message);
}

在mainwindow类中,我有一个连接,但是没有通过插槽接收到信号, device是类mainwindow的字段。

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    fields[LinkAgentType] = TypeOfConnection::EXTERNAL;
    fields[PathToDriver] = "C:/Users/user/Desktop/RouterCpp/RouterCore/build-Driver101-104-Desktop_Qt_5_13_0_MinGW_32_bit-Debug/debug/Driver101-104";
    fields[IpAddr] = "127.0.0.1";
    fields[Port] = 2404;
    fields[TypeOfDevice] = TypeOfDevice::External;
    fields[TypeOfMessage] = TypeOfMessage::Service;
    fields[ConnectionState] = ConnectState::On;

    /*Prepare parameters for the jsonObject*/
    builder = new DeviceBuilder(this);
    builder->setConstructorStructure(fields);
    device = builder->getDeviceObject();
    device->setMessage(fields);

    connect(device.get(),&Device::sendCallBackMessage,
            this,&MainWindow::callBackMessageSlot,Qt::QueuedConnection);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::callBackMessageSlot(QJsonObject _data)
{
    qDebug()<<_data;
}

0 个答案:

没有答案