【问题标题】:Where is Qt’s PointerToMemberFunction defined?Qt 的 PointerToMemberFunction 定义在哪里?
【发布时间】:2015-03-23 18:50:29
【问题描述】:

this question 中,我能够适应QObject 方法

QMetaObject::Connection
QObject::connect(const QObject * sender,
    const char * signal,
    const QObject * receiver,
    const char * method,
    Qt::ConnectionType type = Qt::AutoConnection)

转换为接受QSharedPointer 而不是QObject* 作为其第一个参数的变体:

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)

我想对采用函数指针的版本做同样的事情,

QMetaObject::Connection
QObject::connect(const QObject * sender,
    PointerToMemberFunction signal,
    const QObject * receiver,
    PointerToMemberFunction method,
    Qt::ConnectionType type = Qt::AutoConnection)

但我不知道PointerToMemberFunction 是什么或者它是在哪里定义的!这是什么类型的?

【问题讨论】:

    标签: c++ qt qobject qsharedpointer


    【解决方案1】:

    它只是文档的“糖”,你可以在qobject.h(Qt源代码)中找到真正的定义:

        static QMetaObject::Connection connect(const QObject *sender, const char *signal,
                            const QObject *receiver, const char *member, Qt::ConnectionType = Qt::AutoConnection);
    
        static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
                            const QObject *receiver, const QMetaMethod &method,
                            Qt::ConnectionType type = Qt::AutoConnection);
    
        inline QMetaObject::Connection connect(const QObject *sender, const char *signal,
                            const char *member, Qt::ConnectionType type = Qt::AutoConnection) const;
    
    #ifdef Q_QDOC
        static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection);
        static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor);
        static QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type = Qt::AutoConnection);
    #else
        //Connect a signal to a pointer to qobject member function
        template <typename Func1, typename Func2>
        static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
                                         const typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot,
                                         Qt::ConnectionType type = Qt::AutoConnection)
        {
            typedef QtPrivate::FunctionPointer<Func1> SignalType;
            typedef QtPrivate::FunctionPointer<Func2> SlotType;
    
            Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,
                              "No Q_OBJECT in the class with the signal");
    
            //compilation error if the arguments does not match.
            Q_STATIC_ASSERT_X(int(SignalType::ArgumentCount) >= int(SlotType::ArgumentCount),
                              "The slot requires more arguments than the signal provides.");
            Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<typename SignalType::Arguments, typename SlotType::Arguments>::value),
                              "Signal and slot arguments are not compatible.");
            Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible<typename SlotType::ReturnType, typename SignalType::ReturnType>::value),
                              "Return type of the slot is not compatible with the return type of the signal.");
    
            const int *types = 0;
            if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)
                types = QtPrivate::ConnectionTypes<typename SignalType::Arguments>::types();
    
            return connectImpl(sender, reinterpret_cast<void **>(&signal),
                               receiver, reinterpret_cast<void **>(&slot),
                               new QtPrivate::QSlotObject<Func2, typename QtPrivate::List_Left<typename SignalType::Arguments, SlotType::ArgumentCount>::Value,
                                               typename SignalType::ReturnType>(slot),
                                type, types, &SignalType::Object::staticMetaObject);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多