Attaching signals and slots to an object within a QSharedPointer

前端 未结 2 1684
暖寄归人
暖寄归人 2021-02-14 16:14

My application contained several functions like this:

void SomeClass::set_data_provider(DataProvider *data_provider)
{
    connect(data_provider, SIGNAL(data_ava         


        
2条回答
  •  粉色の甜心
    2021-02-14 16:56

    For completeness, here is an extension of @hank’s answer. I provide six connect-like functions:

    • connect_from_pointer takes a QSharedPointer as its first argument and the usual QObject * as its third argument.
    • connect_to_pointer takes the usual QObject * as its first argument and a QSharedPointer as its third argument.
    • connect_pointers takes QSharedPointers for both parameters.

    There are two versions of each of these: one that accepts the SIGNAL()/SLOT() syntax and one that accepts the (recommended) function pointers.

    These are syntactically atrocious, but that’s templates for you.

    template
    QMetaObject::Connection connect_from_pointer(
        const QSharedPointer& sender,
        const char *signal,
        const QObject *receiver,
        const char *method,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender.data(), signal, receiver, method, type);
    }
    
    template 
    QMetaObject::Connection connect_from_pointer(
        const QSharedPointer::Object>& sender,
        Func1 signal,
        const typename QtPrivate::FunctionPointer::Object *receiver,
        Func2 slot,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender.data(), signal, receiver, slot, type);
    }
    
    template
    QMetaObject::Connection connect_to_pointer(
        const QObject *sender,
        const char *signal,
        const QSharedPointer& receiver,
        const char *method,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender, signal, receiver.data(), method, type);
    }
    
    template 
    QMetaObject::Connection connect_to_pointer(
        const typename QtPrivate::FunctionPointer::Object *sender,
        Func1 signal,
        const QSharedPointer::Object>& receiver,
        Func2 slot,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender, signal, receiver.data(), slot, type);
    }
    
    template
    QMetaObject::Connection connect_pointers(
        const QSharedPointer& sender,
        const char *signal,
        const QSharedPointer& receiver,
        const char *method,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender.data(), signal, receiver.data(), method, type);
    }
    
    template 
    QMetaObject::Connection connect_pointers(
        const QSharedPointer::Object>& sender,
        Func1 signal,
        const QSharedPointer::Object>& receiver,
        Func2 slot,
        Qt::ConnectionType type = Qt::AutoConnection)
    {
        return QObject::connect(sender.data(), signal, receiver.data(), slot, type);
    }
    

提交回复
热议问题