使用boost信号代替qt
using boost signals instead of qt
这是一个很长的机会,但是,遇到了qt的信号形式的约束,插槽无法被模板化,我只是想用boost信号代替它们,这是可行的选择吗?
您不强制使用qt的信号和插槽。从使用Qt与第三方信号和插槽:
可以将Qt与第三方信号/插槽机制一起使用。你甚至可以在同一个项目中使用这两种机制。只要加上下面一行到你的qmake项目(.pro)文件。
CONFIG += no_keywords
它告诉Qt不要定义moc关键字signals, slots和emit,因为这些名字会被第三方库使用,比如Boost。然后继续使用带有no_keywords标志的Qt信号和插槽,只需将源代码中Qt moc关键字的所有使用替换为相应的Qt宏Q_SIGNALS(或Q_SIGNAL), Q_SLOTS(或Q_SIGNAL)Q_SLOT)和Q_EMIT.
有一个完整的说明如何连接boost信号到qt信号。
我在网上找到了这个适配器,但不知道在哪里:
#ifndef _QT_2_FUNC_3_H_
#define _QT_2_FUNC_3_H_
#include <iostream>
#include <boost/function.hpp>
#include <boost/type_traits.hpp>
#include <QObject>
#include <libQtSignalAdapters/QtConnDefault.h>
using namespace boost;
namespace QtSignalAdapters
{
/**
* cond
*/
template<typename SIGNATURE>
class Qt2FuncSlot3
{
public:
typedef function<SIGNATURE> FuncType;
typedef typename function_traits<SIGNATURE>::arg1_type ParmType1;
typedef typename function_traits<SIGNATURE>::arg2_type ParmType2;
typedef typename function_traits<SIGNATURE>::arg3_type ParmType3;
Qt2FuncSlot3(const FuncType& func) :
func_(func)
{
}
void call(QObject* sender, void **arguments)
{
ParmType1* a1 = reinterpret_cast<ParmType1*>(arguments[1]);
ParmType2* a2 = reinterpret_cast<ParmType2*>(arguments[2]);
ParmType3* a3 = reinterpret_cast<ParmType3*>(arguments[3]);
if ( func_ )
func_(*a1,*a2, *a3);
}
private:
FuncType func_;
};
/**
* endcond
*/
template<typename SIGNATURE>
class Qt2Func3 : public QObject, public QtConnDefault
{
public:
typedef function<SIGNATURE> FuncType;
typedef typename function_traits<SIGNATURE>::arg1_type ParmType;
Qt2Func3(QObject* qobject, int signalIdx, const FuncType& func,
bool initiallyConnected=true) :
QObject(qobject),
QtConnDefault(qobject, signalIdx),
func_(func)
{
//
// Get the next usable slot ID on this...
//
slotIdx_ = metaObject()->methodCount();
//
// Create a slot to handle invoking the boost::function object.
//
slot_ = new Qt2FuncSlot3<SIGNATURE>(func);
if ( initiallyConnected )
connect();
}
~Qt2Func3()
{
delete slot_;
}
int qt_metacall(QMetaObject::Call c, int id, void **arguments)
{
id = QObject::qt_metacall(c, id, arguments);
if ( id < 0 || c != QMetaObject::InvokeMetaMethod )
return id;
slot_->call(sender(), arguments);
return -1;
}
void connect()
{
connect_();
}
void disconnect()
{
disconnect_();
}
private:
void connect_()
{
connected_ =
QMetaObject::connect(qobject_, signalIdx_, this, slotIdx_);
}
void disconnect_()
{
connected_ =
!QMetaObject::disconnect(qobject_, signalIdx_, this, slotIdx_);
}
FuncType func_;
Qt2FuncSlot3<SIGNATURE>* slot_;
};
}
#endif
所以,基本上你必须重新实现qt_metacall
函数
相关文章:
- Qt 信号/插槽问题
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- C++Qt信号和插槽
- 使用std::move将std::unique_ptr作为qt信号参数传递
- 使用std::bind将Qt信号连接到插槽
- 如果代码中没有连接任何插槽,是否有理由发出Qt信号?
- 文档在哪里说明如果参数不是从末端删除参数,则无法从QT信号插槽连接中删除参数?
- 按钮组上的 Qt 信号未连接
- 通过排队的QT信号插槽连接发送QSHAREDPOINTERS的QVECTOR
- QT信号和插槽无法提供错误消息
- 如何监控Qt信号事件队列深度
- QT信号在虚拟机中不连接
- 为什么事件队列未处理我的QT信号
- 如何在打印机完成打印时从打印机获取Qt信号?
- 这是在Qt信号和插槽中使用参数调用函数的好方法吗?
- 删除 QObject 后 Qt 信号仍在触发
- Qt c++信号槽 - 清除信号所必需的
- 一个类中的QT信号/插槽,但从不同的线程发出
- QT:C 信号到QML连接
- 来自静态方法的QT信号