gpt4 book ai didi

c++ - Qt - 从回调中发出信号

转载 作者:行者123 更新时间:2023-11-28 05:19:45 25 4
gpt4 key购买 nike

我在我的 Qt 应用程序中使用 BGLib 通过 BLE 进行通信。

在主线程中运行 GUI (sensornode_gui.cpp)。通过单击按钮,不同的线程开始扫描 BLE 设备 (ble_connection)。我从回调函数中获取结果信息,例如 void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)。例如,如何从该回调函数发出信号以使用 msg 中的某些属性更新 GUI?

main.cpp

#include "sensornode_gui.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SensorNode_GUI w;
w.show();
return a.exec();
}

sensornode_gui.h

#ifndef SENSORNODE_GUI_H
#define SENSORNODE_GUI_H

#include <QMainWindow>
#include <QThread>
#include "ble_connection.h"

namespace Ui {
class SensorNode_GUI;
}

class SensorNode_GUI : public QMainWindow
{
Q_OBJECT

public:
explicit SensorNode_GUI(QWidget *parent = 0);
~SensorNode_GUI();
void printText(std::string s);

private slots:
void on_pushButtonStartScanning_clicked();

private:
Ui::SensorNode_GUI *ui;
QThread *thread;
BLE_Connection *ble_worker;

};

#endif // SENSORNODE_GUI_H

sensornode_gui.cpp

#include "sensornode_gui.h"
#include "ui_sensornode_gui.h"

SensorNode_GUI::SensorNode_GUI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SensorNode_GUI)
{
ui->setupUi(this);

thread = new QThread();
ble_worker = new BLE_Connection();
ble_worker->setGUI(this);

ble_worker->moveToThread(thread);
connect(ble_worker, SIGNAL(valueChanged(QString)), ui->textEdit, SLOT(append(QString)));
connect(ble_worker, SIGNAL(workRequested()), thread, SLOT(start()));
connect(thread, SIGNAL(started()), ble_worker, SLOT(doWork()));
}

SensorNode_GUI::~SensorNode_GUI()
{
delete thread;
delete ble_worker;

delete ui;
}

void SensorNode_GUI::printText(std::string s){
ui->textEdit->append(QString::fromStdString(s));
}

void SensorNode_GUI::on_pushButtonStartScanning_clicked()
{
ble_worker->requestWork();
}

ble_connection.h

#ifndef BLE_CONNECTION_H
#define BLE_CONNECTION_H

#include <QObject>
#include <QMutex>
#include <QDebug>
#include <QThread>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "cmd_def.h"
#include "apitypes.h"
#include "sensornode_gui.h"

class SensorNode_GUI;

class BLE_Connection : public QObject
{
Q_OBJECT

public:
explicit BLE_Connection(QObject *parent = 0);
static void output(uint8,uint8*,uint16,uint8*);
int read_message();
void connect();
void disconnect();

void requestWork();

private:
bool _abort;
bool _working;
QMutex mutex;

signals:
void workRequested();
void valueChanged(const QString &value);

public slots:
void doWork();
};

#endif // BLE_CONNECTION_H

ble_connection.cpp

#include "ble_connection.h"

volatile HANDLE serial_handle;
uint8 connection_handle;

BLE_Connection::BLE_Connection(QObject *parent) :
QObject(parent)
{
char str[80];
snprintf(str,sizeof(str)-1,"\\\\.\\%s","COM3");
//open I/O device
...

bglib_output = output;

}


void BLE_Connection::setGUI(SensorNode_GUI *g)
{
gui = g;
}

void BLE_Connection::requestWork()
{
mutex.lock();
_working = true;
_abort = false;
qDebug()<<"Request worker start in Thread "<<thread()->currentThreadId();
mutex.unlock();

emit workRequested();
}


void BLE_Connection::doWork()
{
while(true){
read_message();
qDebug() << "Within doWork";
emit valueChanged(QString::fromStdString("Test"));
}
}

void BLE_Connection::output(uint8 len1,uint8* data1,uint16 len2,uint8* data2)
{
...
}

int BLE_Connection::read_message()
{
DWORD rread;
const struct ble_msg *apimsg;
struct ble_header apihdr;
unsigned char data[256];//enough for BLE
//read header

if(!ReadFile(serial_handle,
(unsigned char*)&apihdr,
4,
&rread,
NULL))
{
return GetLastError();
}
if(!rread)return 0;
//read rest if needed
if(apihdr.lolen)
{
if(!ReadFile(serial_handle,
data,
apihdr.lolen,
&rread,
NULL))
{
return GetLastError();
}
}
apimsg=ble_get_msg_hdr(apihdr);
if(!apimsg)
{
printf("ERROR: Message not found:%d:%d\n",(int)apihdr.cls,(int)apihdr.command);
return -1;
}
apimsg->handler(data);

return 0;
}


//=============FUNCTIONS FOR HANDELING EVENTS AND RESPONSES====================


void ble_evt_gap_scan_response(const struct ble_msg_gap_scan_response_evt_t *msg)
{
int i;
std::string buffAsStdStr;
char buff[100];
char *name = NULL;
for(i=0;i<6;i++){
snprintf(buff, sizeof(buff), "%02x%s",msg->sender.addr[5-i],i<5?":":"");
buffAsStdStr += buff;
}


snprintf(buff, sizeof(buff), "\t%d",msg->rssi);
buffAsStdStr += buff;
//HERE I WANT TO EMIT THE SIGNAL VALUECHANGED
//emit valueChanged(QString::fromStdString(buffAsStdStr));

free(name);
}

提前致谢。

最佳答案

我在使用不同的库时遇到了同样的问题。我已经通过用我的(CameraWrapper)包装库类解决了这个问题,并将此类的对象放入我的类(MeasurementSystem)中继承自QObject。因此,当此父类为发送回调的类创建包装器时,它会将指针传递给自身 QObject*。子框架 (CameraWrapper) 存储此指针并在回调中发送它。

看起来像这样:

测量系统.h

#ifndef MEASUREMENTSYSTEM_H
#define MEASUREMENTSYSTEM_H
#include <vector>
#include "camerawrapper.h"
#include "qobject.h"

class MeasurementSystem : public QObject
{
Q_OBJECT
public:
MeasurementSystem();
enum CamAlign {caTopInner = 0,caTopOuter,caBottomInner,caBottomOuter,caSide}; //cameras alignment in optical sysem


void Connect();
void Disconnect();
signals:
void sigDataReceived(int,std::vector<int>);

private:
std::vector<CameraWrapper*> _cams;
friend void DataReceived(QObject * Sender, int &, std::vector<int>&); //callback function for receiving data from any camera
};

#endif // MEASUREMENTSYSTEM_H

测量系统.cpp

#include "measurementsystem.h"
#include <QApplication>
MeasurementSystem::MeasurementSystem()
{
QString path = QApplication::applicationDirPath();
_cams.push_back(new CameraWrapper(this,*DataReceived,(path+"/CameraSettings/Ranger D50 TopInner.icx").toStdString(),"192.168.1.11","CamTopInner",caTopInner));
_cams.push_back(new CameraWrapper(this,*DataReceived,(path+"/CameraSettings/Ranger D50 TopInner.icx").toStdString(),"192.168.1.12","CamTopOuter",caTopOuter));
_cams.push_back(new CameraWrapper(this,*DataReceived,(path+"/CameraSettings/Ranger D50 TopInner.icx").toStdString(),"192.168.1.13","CamBottomInner",caBottomInner));
_cams.push_back(new CameraWrapper(this,*DataReceived,(path+"/CameraSettings/Ranger D50 TopInner.icx").toStdString(),"192.168.1.14","CamBottomOuter",caBottomOuter));
_cams.push_back(new CameraWrapper(this,*DataReceived,(path+"/CameraSettings/Ranger D50 TopInner.icx").toStdString(),"192.168.1.15","CamSide",caSide));
}

void MeasurementSystem::Connect()
{
foreach (CameraWrapper* cam, _cams)
{
cam->Connect();
}
}

void MeasurementSystem::Disconnect()
{
foreach (CameraWrapper* cam, _cams)
{
cam->Disconnect();
}
}

void DataReceived(QObject * Sender, int & CamId, std::vector<int> & Profile)
{
MeasurementSystem * CamSys = reinterpret_cast<MeasurementSystem*>(Sender);
emit CamSys->sigDataReceived(CamId,Profile);
}

CameraWrapper.h

#ifndef CAMERAWRAPPER_H
#define CAMERAWRAPPER_H

#include <QObject>
#include <icamerasystem.h>
#include <icamerasystemsubscriber.h>
#include <icon_error.h>
#include <windows.h>
typedef void CallbackDataT(QObject*,int&,std::vector<int>&);

class CameraWrapper : public icon::ICameraSystemSubscriber, public icon::ErrorHandler
{
public:
CameraWrapper(QObject * Parent, CallbackDataT callback, std::string ConfigFileIcx, std::string IpAddr, std::string Name, int Id = -1);
void Connect();
void Disconnect();
int AccessData(const icon::IconBuffer *buffer, std::string& resultString);
virtual void onData(const icon::IconBuffer * buffer, const icon::IGrabStatus * status);
virtual void onError(const icon::IError &error){};
virtual void onStateChanged(const icon::IState * state){};

private:
//IconAPI objects
icon::ErrorCode _res; //result of some camera operations
icon::ICameraSystem * _camSystem; //the camera itself

//callbacks
void (*_callbackData)(QObject*,int&, std::vector<int>&); //called when buffer is parsed

//proprieties
int _id; //camera identifier
std::string _name; //camera name

//parent
QObject * _parent;
};
#endif // CAMERAWRAPPER_H

CameraWrapper.cpp

#include "camerawrapper.h"
#include <qdebug.h> //qdebug()


CameraWrapper::CameraWrapper(QObject * Parent, CallbackDataT Callback, std::string ConfigFileIcx, std::string IpAddr, std::string Name, int Id):
_parent(Parent), _callbackData(Callback), _id(Id), _name(Name)
{
//Create the camera system object
_camSystem = icon::createCameraSystem(icon::ICameraSystem::ETHERNET_CAMERA, Name.c_str());
//smth else
}

void CameraWrapper::Connect()
{
_res = _camSystem->subscribe(*this);
_res = _camSystem->connect();
_res = _camSystem->start();
}

void CameraWrapper::Disconnect()
{
_res = _camSystem->stop();
_res = _camSystem->disconnect();
}

int CameraWrapper::AccessData(const icon::IconBuffer * buffer, std::string& resultString)
{
std::vector<int> vProfile;
// Loop through all components in the buffer
const icon::DataFormat *dataFormat = buffer->getDataFormat();
for (unsigned int compIndex = 0; compIndex < dataFormat->numComponents(); compIndex++)
{
// Get handle to component.
const icon::Component *component = dataFormat->getComponent(compIndex);

// Loop through all subcomponents of the component.
for (unsigned int subCompIndex = 0; subCompIndex < component->numSubComponents(); subCompIndex++)
{
const icon::SubComponent *subComponent = component->getSubComponent(subCompIndex);
unsigned int subCompWidth = subComponent->getWidth();

const unsigned short *wordData;
unsigned short wordValue;
buffer->getReadPointer(component->getName().c_str(), subComponent->getName().c_str(), 0, wordData);
//rsv:push current scan into vector(profile view)
for (size_t i = 0; i < subCompWidth; i++)
{
wordValue = *(wordData + i);
vProfile.push_back(static_cast<int>(wordValue));
}
}
}
//callback is here
_callbackData(_parent,_id,vProfile);
return 0;
}

void CameraWrapper::onData(const icon::IconBuffer * buffer, const icon::IGrabStatus * status)
{
if (!(status->allOK()))
{
return;
}
AccessData(buffer, outString);
}

关于c++ - Qt - 从回调中发出信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41771848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com