将C-Stlye回调事件到QT5信号的最佳方法
在我的QT5项目中,我通过C风格库与一件硬件进行交互,该库通过C风格回调方法为我提供了事件更新。回调运行在库线程上,我想立即返回控件。我的目标是在硬件库为我提供事件更新时转发信号。我找到了下面显示的解决方案,但我不确定这是最好的方法。有更好的方法吗?使用QT :: PostEvent()在我的寄存器回调中可以更好地工作吗?那会是什么样?
// hw_api.h
// C-Style library
class HWLIB {
public:
static HWLIB& GetInstance();
void (*_update1CB)(uint8_t, uint8_t);
void (*_update2CB)(double, double);
void (*_update3CB)(uint8_t, uint8_t);
void SetUpdate1CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {
_update1CB = cbFunc;
}
void SetUpdate2CB(void (cbFunc)(double old_value, double new_value)) {...}
void SetUpdate3CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {...}
private:
HWLIB();
};
// hw_api_interface.h
class hwi : public QObject {
Q_Object
signals:
// Receiver must use Qt::QueuedConnection
void status1Changed(bool);
void status2Changed(double);
void status3Changed(bool);
private:
HWLILB m_lib;
static std::function<void(const bool &)> update1Delegate;
static std::function<void(const double &)> update2Delegate;
static std::function<void(const bool &)> update3Delegate;
// Static methods to use in c-style callback but doesn't have access to class signals and member variables
static void update1CB(uint8_t, uint8_t);
static void update2CB(double, double);
static void update2CB(uint8_t, uint8_t);
};
// hw_api_interface.cpp
// Use static functions to call class member variables to have access to signals
std::function<void(const bool &)> hwi::update1Delegate{};
std::function<void(const double &)> hwi::update2Delegate{};
std::function<void(const bool &)> hwi::update3Delegate{};
hwi::hwi() : m_lib(HWLIB::GetInstance())
{
update1Delegate = [&](auto value) { emit status1Changed(value); };
update2Delegate = [&](auto value) { emit status2Changed(value); };
update3Delegate = [&](auto value) { emit status3Changed(value); };
// Register call backs
m_lib.SetUpdate1CB(update1CB);
m_lib.SetUpdate2CB(update2CB);
m_lib.SetUpdate3CB(update3CB);
}
void hwi::update1CB(uint8_t old_value, uint8_t new_value)
{
update1Delegate(static_cast<bool>(new_value));
}
void hwi::update2CB(double old_value, double new_value)
{
update2Delegate(new_value);
}
void hwi::update3CB(uint8_t old_value, uint8_t new_value)
{
update3Delegate(static_cast<bool>(new_value));
}
// void hwi::update3CB(uint8_t old_value, uint8_t new_value)
// {
// Qt::postEvent(update1Changed, new_value); // new_value wrapped in QEvent?
// }
In my Qt5 project, I am interacting with a piece of hardware through a C-Style library that provides me with event updates through a C-Style callback method. The callback runs on the library thread for which I'd like to return control immediately. My goal is to forward a signal when the hardware library provides me with an event update. I have found a solution shown below but I am not sure if it's the best way. Is there a better method? Would using Qt::postEvent() work better within my registerd callback instead? What would that look like?
// hw_api.h
// C-Style library
class HWLIB {
public:
static HWLIB& GetInstance();
void (*_update1CB)(uint8_t, uint8_t);
void (*_update2CB)(double, double);
void (*_update3CB)(uint8_t, uint8_t);
void SetUpdate1CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {
_update1CB = cbFunc;
}
void SetUpdate2CB(void (cbFunc)(double old_value, double new_value)) {...}
void SetUpdate3CB(void (cbFunc)(uint8_t old_value, uint8_t new_value)) {...}
private:
HWLIB();
};
// hw_api_interface.h
class hwi : public QObject {
Q_Object
signals:
// Receiver must use Qt::QueuedConnection
void status1Changed(bool);
void status2Changed(double);
void status3Changed(bool);
private:
HWLILB m_lib;
static std::function<void(const bool &)> update1Delegate;
static std::function<void(const double &)> update2Delegate;
static std::function<void(const bool &)> update3Delegate;
// Static methods to use in c-style callback but doesn't have access to class signals and member variables
static void update1CB(uint8_t, uint8_t);
static void update2CB(double, double);
static void update2CB(uint8_t, uint8_t);
};
// hw_api_interface.cpp
// Use static functions to call class member variables to have access to signals
std::function<void(const bool &)> hwi::update1Delegate{};
std::function<void(const double &)> hwi::update2Delegate{};
std::function<void(const bool &)> hwi::update3Delegate{};
hwi::hwi() : m_lib(HWLIB::GetInstance())
{
update1Delegate = [&](auto value) { emit status1Changed(value); };
update2Delegate = [&](auto value) { emit status2Changed(value); };
update3Delegate = [&](auto value) { emit status3Changed(value); };
// Register call backs
m_lib.SetUpdate1CB(update1CB);
m_lib.SetUpdate2CB(update2CB);
m_lib.SetUpdate3CB(update3CB);
}
void hwi::update1CB(uint8_t old_value, uint8_t new_value)
{
update1Delegate(static_cast<bool>(new_value));
}
void hwi::update2CB(double old_value, double new_value)
{
update2Delegate(new_value);
}
void hwi::update3CB(uint8_t old_value, uint8_t new_value)
{
update3Delegate(static_cast<bool>(new_value));
}
// void hwi::update3CB(uint8_t old_value, uint8_t new_value)
// {
// Qt::postEvent(update1Changed, new_value); // new_value wrapped in QEvent?
// }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论