Attaching signals and slots to an object within a QSharedPointer

前端 未结 2 1685
暖寄归人
暖寄归人 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:55

    You can create a custom connect function:

    template bool
    my_connect(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);
    }
    

    And use it like this:

    QSharedPointer shared(new MyObject);
    my_connect(shared, SIGNAL(my_signal()), this, SLOT(my_slot()));
    

    The only problem is that in both yours and mine solutions you will lose Qt Creator autocomplete for the native connect function.

    P.S. As for me, I wouldn't change your code. I think it's fine :)

提交回复
热议问题