将信号和插槽附加到QSharedPointer中的对象

时间:2015-02-10 21:03:33

标签: c++ qt qsharedpointer

我的应用程序包含几个这样的函数:

void SomeClass::set_data_provider(DataProvider *data_provider)
{
    connect(data_provider, SIGNAL(data_available(int)),
        this, SLOT(data_available(int)));
}

为避免传递原始指针,我已将所有DataProvider *更改为QSharedPointer<DataProvider>。后者几乎是前者的替代品,除了你不能将QSharedPointer传递给QObject::connect。我通过从QSharedPointer中提取原始指针来解决这个问题:

void SomeClass::set_data_provider(QSharedPointer<DataProvider> data_provider)
{
    connect(data_provider.data(), SIGNAL(data_available(int)),
        this, SLOT(data_available(int)));
}

这似乎工作得很好,但它看起来不够优雅,我担心像这样访问原始指针。有没有更好的方法连接到在QSharedPointer中传递的对象?

2 个答案:

答案 0 :(得分:2)

您可以创建自定义连接功能:

template<class T> bool
my_connect(const QSharedPointer<T> &sender,
           const char *signal,
           const QObject *receiver,
           const char *method,
           Qt::ConnectionType type = Qt::AutoConnection)
{
    return QObject::connect(sender.data(), signal, receiver, method, type);
}

并像这样使用它:

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

唯一的问题是,在您和我的解决方案中,您将失去本机连接功能的Qt Creator自动完成功能。

P.S。至于我,我不会改变你的代码。我认为没关系:)。

答案 1 :(得分:1)

为了完整起见,这是@ hank答案的扩展。我提供了六个connect - 类似函数:

  • connect_from_pointer将QSharedPointer作为其第一个参数,并将通常的QObject *作为其第三个参数。
  • connect_to_pointer将通常的QObject *作为其第一个参数,将QSharedPointer作为其第三个参数。
  • connect_pointers为两个参数提供QSharedPointers。

每个版本都有两个版本:一个接受SIGNAL() / SLOT()语法,另一个接受(推荐)函数指针。

这些在语法上是残暴的,但这是你的模板。

template<class T>
QMetaObject::Connection connect_from_pointer(
    const QSharedPointer<T>& 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 <typename Func1, typename Func2>
QMetaObject::Connection connect_from_pointer(
    const QSharedPointer<typename QtPrivate::FunctionPointer<Func1>::Object>& sender,
    Func1 signal,
    const typename QtPrivate::FunctionPointer<Func2>::Object *receiver,
    Func2 slot,
    Qt::ConnectionType type = Qt::AutoConnection)
{
    return QObject::connect(sender.data(), signal, receiver, slot, type);
}

template<class T>
QMetaObject::Connection connect_to_pointer(
    const QObject *sender,
    const char *signal,
    const QSharedPointer<T>& receiver,
    const char *method,
    Qt::ConnectionType type = Qt::AutoConnection)
{
    return QObject::connect(sender, signal, receiver.data(), method, type);
}

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

template<class T, class U>
QMetaObject::Connection connect_pointers(
    const QSharedPointer<T>& sender,
    const char *signal,
    const QSharedPointer<U>& receiver,
    const char *method,
    Qt::ConnectionType type = Qt::AutoConnection)
{
    return QObject::connect(sender.data(), signal, receiver.data(), method, type);
}

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