Qt - 从回调发出信号
Qt - Emit signal from callback
我在我的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?
主.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:%dn",(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)包装库类来解决这个问题,并将这个类的对象放入从QObject继承的我的类(测量系统)中。因此,当这个父类为发送回调的类创建包装器时,它会将指针传递给自身 QObject*。子 chass (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
相机包装器.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);
}
您可以在"ble_connection.h"中声明一个全局变量
static bool messageCaptured;
static std::string message;
在BLE_Connection类构造函数初始化值为 false
;
messageCaptured = false;
现在;如果你的回调函数在任何时候被调用,你应该设置
messageCaptured = true;
在你的回调函数中。
现在,您可以在doWork函数中发出捕获的消息
void BLE_Connection::doWork()
{
while(true){
read_message();
qDebug() << "Within doWork";
if( messageCaptured ){
/// do your process
emit valueChanged(QString::fromStdString(message);
messageCaptured = false;
}
}
}
在回调函数中捕获消息,如果捕获过程在之前完成
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;
// if message captured before you can take it
if( !messageCaptured ){
message = buffAsStdStr;
messageCaptured = true;
}
free(name);
}
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- 如何使用来自外部脚本的回调发送信号
- Qt - 从回调发出信号
- 当通过代码触发回调时,抑制GTK信号
- C++信号回调(类似javascript)
- 当通知程序/信号超出范围时,在提升绑定中解除分配回调侦听器对象
- 使用 Qt 信号和插槽实现 c++ 回调
- c++定时器实现-将一个成员函数分配给信号回调函数指针
- 为什么信号和插槽比普通的旧回调更好