C++ 使用boost信号代替qt

C++ 使用boost信号代替qt,c++,qt,boost,qt4,C++,Qt,Boost,Qt4,这是一个很长的机会,但是,在遇到qt信号形式的约束后,插槽无法模板化,我只是想用boost信号替换它们,这是可行的选择吗?您不必被迫使用qt信号和插槽。发件人: 可以将Qt与第三方信号/插槽机制一起使用。你 甚至可以在同一个项目中使用这两种机制。只需添加 将以下行添加到qmake项目(.pro)文件 它告诉Qt不要定义moc关键字信号、插槽和发射, 因为这些名称将由第三方库使用,例如Boost。 然后继续使用Qt信号和带有no_关键字标志的插槽, 只需将源代码中Qt moc关键字的所有用法替换为

这是一个很长的机会,但是,在遇到qt信号形式的约束后,插槽无法模板化,我只是想用boost信号替换它们,这是可行的选择吗?

您不必被迫使用qt信号和插槽。发件人:

可以将Qt与第三方信号/插槽机制一起使用。你 甚至可以在同一个项目中使用这两种机制。只需添加 将以下行添加到qmake项目(.pro)文件

它告诉Qt不要定义moc关键字信号、插槽和发射, 因为这些名称将由第三方库使用,例如Boost。 然后继续使用Qt信号和带有no_关键字标志的插槽, 只需将源代码中Qt moc关键字的所有用法替换为 相应的Qt宏Q_信号(或Q_信号)、Q_插槽(或 Q_插槽)和Q_发射

关于如何将升压信号连接到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
\ifndef\u QT\u 2\u FUNC\u 3\H_
#定义_QT_2_FUNC_3_H_
#包括
#包括
#包括
#包括
#包括
使用名称空间boost;
名称空间信号适配器
{
/**
*\cond
*/
模板
类Qt2FuncSlot3
{
公众:
typedef函数FuncType;
typedef typename函数_traits::arg1_type ParmType1;
typedef typename函数_traits::arg2_type ParmType2;
typedef typename函数_traits::arg3_type ParmType3;
Qt2FuncSlot3(常量FuncType&func):
func(func)
{
}
void调用(QObject*发送方,void**参数)
{
ParmType1*a1=重新解释(参数[1]);
ParmType2*a2=重新解释(参数[2]);
ParmType3*a3=重新解释(参数[3]);
if(func_)
函数(*a1,*a2,*a3);
}
私人:
func类型func;
};
/**
*\endond
*/
模板
类Qt2Func3:公共QObject,公共QtConnDefault
{
公众:
typedef函数FuncType;
typedef typename函数_traits::arg1_type ParmType;
Qt2Func3(QObject*QObject,int-signalIdx,const-FuncType&func,
bool initiallyConnected=true):
QoObject(QoObject),
QtConnDefault(qobject,signalIdx),
func(func)
{
//
//获取此上的下一个可用插槽ID。。。
//
SLOTIX=元对象()->methodCount();
//
//创建一个插槽来处理调用boost::function对象的操作。
//
插槽=新Qt2FuncSlot3(func);
如果(初始连接)
connect();
}
~Qt2Func3()
{
删除时隙;
}
int qt_metacall(QMetaObject::Call c,int id,void**arguments)
{
id=QObject::qt_元调用(c,id,参数);
if(id<0 | | c!=QMetaObject::InvokeMetMethod)
返回id;
插槽->调用(发送方(),参数);
返回-1;
}
void connect()
{
连接();
}
无效断开连接()
{
断开连接();
}
私人:
void connect_u389;()
{
连接的=
QMetaObject::connect(qobject、signalIdx、this、slotIdx);
}
无效断开连接
{
连接的=
!QMetaObject::disconnect(qobject、signalIdx、this、slotIdx);
}
func类型func;
Qt2FuncSlot3*插槽;
};
}
#恩迪夫

所以,基本上你必须重新实现
qt\u metacall
函数。

我们怎么知道?一般来说,Boost.Signals绝对没有问题,但这取决于您决定Boost是否适合您的特定问题。Qt 5支持将信号连接到任意现成的函数-参见Qt 4,有一些适配器库。我尝试在@RobertKnight上找到其他人的链接,这应该是一个答案(如果你问我的话,这是一个很好的答案)。顺便说一句,他们最终做出了一个关于信号/时隙的好决定
#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