/**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. ** Copyright (C) 2013 Olivier Goffart ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL21$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 or version 3 as published by the Free ** Software Foundation and appearing in the file LICENSE.LGPLv21 and ** LICENSE.LGPLv3 included in the packaging of this file. Please review the ** following information to ensure the GNU Lesser General Public License ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** As a special exception, The Qt Company gives you certain additional ** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QOBJECT_H #define QOBJECT_H #ifndef QT_NO_QOBJECT #include #include #include #include #ifdef QT_INCLUDE_COMPAT #include #endif #include #include #include QT_BEGIN_NAMESPACE class QEvent; class QTimerEvent; class QChildEvent; struct QMetaObject; class QVariant; class QObjectPrivate; class QObject; class QThread; class QWidget; #ifndef QT_NO_REGEXP class QRegExp; #endif #ifndef QT_NO_REGULAREXPRESSION class QRegularExpression; #endif #ifndef QT_NO_USERDATA class QObjectUserData; #endif struct QDynamicMetaObjectData; typedef QList QObjectList; Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QMetaObject &mo, QList *list, Qt::FindChildOptions options); Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegExp &re, const QMetaObject &mo, QList *list, Qt::FindChildOptions options); Q_CORE_EXPORT void qt_qFindChildren_helper(const QObject *parent, const QRegularExpression &re, const QMetaObject &mo, QList *list, Qt::FindChildOptions options); Q_CORE_EXPORT QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo, Qt::FindChildOptions options); class Q_CORE_EXPORT QObjectData { public: virtual ~QObjectData() = 0; QObject *q_ptr; QObject *parent; QObjectList children; uint isWidget : 1; uint blockSig : 1; uint wasDeleted : 1; uint isDeletingChildren : 1; uint sendChildEvents : 1; uint receiveChildEvents : 1; uint isWindow : 1; //for QWindow uint unused : 25; int postedEvents; QDynamicMetaObjectData *metaObject; QMetaObject *dynamicMetaObject() const; }; class Q_CORE_EXPORT QObject { Q_OBJECT Q_PROPERTY(QString objectName READ objectName WRITE setObjectName NOTIFY objectNameChanged) Q_DECLARE_PRIVATE(QObject) public: Q_INVOKABLE explicit QObject(QObject *parent=0); virtual ~QObject(); virtual bool event(QEvent *); virtual bool eventFilter(QObject *, QEvent *); #ifdef Q_QDOC static QString tr(const char *sourceText, const char *comment = 0, int n = -1); static QString trUtf8(const char *sourceText, const char *comment = 0, int n = -1); virtual const QMetaObject *metaObject() const; static const QMetaObject staticMetaObject; #endif #ifdef QT_NO_TRANSLATION static QString tr(const char *sourceText, const char * = 0, int = -1) { return QString::fromUtf8(sourceText); } #if QT_DEPRECATED_SINCE(5, 0) QT_DEPRECATED static QString trUtf8(const char *sourceText, const char * = 0, int = -1) { return QString::fromUtf8(sourceText); } #endif #endif //QT_NO_TRANSLATION QString objectName() const; void setObjectName(const QString &name); inline bool isWidgetType() const { return d_ptr->isWidget; } inline bool isWindowType() const { return d_ptr->isWindow; } inline bool signalsBlocked() const { return d_ptr->blockSig; } bool blockSignals(bool b); QThread *thread() const; void moveToThread(QThread *thread); int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer); void killTimer(int id); template inline T findChild(const QString &aName = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename QtPrivate::remove_cv::type>::type ObjType; return static_cast(qt_qFindChild_helper(this, aName, ObjType::staticMetaObject, options)); } template inline QList findChildren(const QString &aName = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename QtPrivate::remove_cv::type>::type ObjType; QList list; qt_qFindChildren_helper(this, aName, ObjType::staticMetaObject, reinterpret_cast *>(&list), options); return list; } #ifndef QT_NO_REGEXP template inline QList findChildren(const QRegExp &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename QtPrivate::remove_cv::type>::type ObjType; QList list; qt_qFindChildren_helper(this, re, ObjType::staticMetaObject, reinterpret_cast *>(&list), options); return list; } #endif #ifndef QT_NO_REGULAREXPRESSION template inline QList findChildren(const QRegularExpression &re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const { typedef typename QtPrivate::remove_cv::type>::type ObjType; QList list; qt_qFindChildren_helper(this, re, ObjType::staticMetaObject, reinterpret_cast *>(&list), options); return list; } #endif inline const QObjectList &children() const { return d_ptr->children; } void setParent(QObject *); void installEventFilter(QObject *); void removeEventFilter(QObject *); 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 static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer::Object *receiver, Func2 slot, Qt::ConnectionType type = Qt::AutoConnection) { typedef QtPrivate::FunctionPointer SignalType; typedef QtPrivate::FunctionPointer SlotType; Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::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::value), "Signal and slot arguments are not compatible."); Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible::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::types(); return connectImpl(sender, reinterpret_cast(&signal), receiver, reinterpret_cast(&slot), new QtPrivate::QSlotObject::Value, typename SignalType::ReturnType>(slot), type, types, &SignalType::Object::staticMetaObject); } //connect to a function pointer (not a member) template static inline typename QtPrivate::QEnableIf::ArgumentCount) >= 0, QMetaObject::Connection>::Type connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, Func2 slot) { return connect(sender, signal, sender, slot, Qt::DirectConnection); } //connect to a function pointer (not a member) template static inline typename QtPrivate::QEnableIf::ArgumentCount) >= 0 && !QtPrivate::FunctionPointer::IsPointerToMemberFunction, QMetaObject::Connection>::Type connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type = Qt::AutoConnection) { typedef QtPrivate::FunctionPointer SignalType; typedef QtPrivate::FunctionPointer SlotType; Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::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::value), "Signal and slot arguments are not compatible."); Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible::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::types(); return connectImpl(sender, reinterpret_cast(&signal), context, 0, new QtPrivate::QStaticSlotObject::Value, typename SignalType::ReturnType>(slot), type, types, &SignalType::Object::staticMetaObject); } //connect to a functor template static inline typename QtPrivate::QEnableIf::ArgumentCount == -1, QMetaObject::Connection>::Type connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, Func2 slot) { return connect(sender, signal, sender, slot, Qt::DirectConnection); } //connect to a functor, with a "context" object defining in which event loop is going to be executed template static inline typename QtPrivate::QEnableIf::ArgumentCount == -1, QMetaObject::Connection>::Type connect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const QObject *context, Func2 slot, Qt::ConnectionType type = Qt::AutoConnection) { #if defined (Q_COMPILER_DECLTYPE) && defined (Q_COMPILER_VARIADIC_TEMPLATES) typedef QtPrivate::FunctionPointer SignalType; const int FunctorArgumentCount = QtPrivate::ComputeFunctorArgumentCount::Value; Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0), "Signal and slot arguments are not compatible."); const int SlotArgumentCount = (FunctorArgumentCount >= 0) ? FunctorArgumentCount : 0; typedef typename QtPrivate::FunctorReturnType::Value>::Value SlotReturnType; #else // Without variadic template, we don't detect the best overload of operator(). We just // assume there is only one simple operator() and connect to &Func2::operator() /* If you get an error such as: couldn't deduce template parameter 'Func2Operator' or cannot resolve address of overloaded function It means the functor does not have a single operator(). Functors with overloaded or templated operator() are only supported if the compiler supports C++11 variadic templates */ #ifndef Q_COMPILER_DECLTYPE //Workaround the lack of decltype using another function as indirection return connect_functor(sender, signal, context, slot, &Func2::operator(), type); } template static inline QMetaObject::Connection connect_functor(const QObject *sender, Func1 signal, const QObject *context, Func2 slot, Func2Operator, Qt::ConnectionType type) { typedef QtPrivate::FunctionPointer SlotType ; #else typedef QtPrivate::FunctionPointer SlotType ; #endif typedef QtPrivate::FunctionPointer SignalType; typedef typename SlotType::ReturnType SlotReturnType; const int SlotArgumentCount = SlotType::ArgumentCount; Q_STATIC_ASSERT_X(int(SignalType::ArgumentCount) >= SlotArgumentCount, "The slot requires more arguments than the signal provides."); Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments::value), "Signal and slot arguments are not compatible."); #endif Q_STATIC_ASSERT_X((QtPrivate::AreArgumentsCompatible::value), "Return type of the slot is not compatible with the return type of the signal."); Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value, "No Q_OBJECT in the class with the signal"); const int *types = 0; if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection) types = QtPrivate::ConnectionTypes::types(); return connectImpl(sender, reinterpret_cast(&signal), context, 0, new QtPrivate::QFunctorSlotObject::Value, typename SignalType::ReturnType>(slot), type, types, &SignalType::Object::staticMetaObject); } #endif //Q_QDOC static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member); static bool disconnect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member); inline bool disconnect(const char *signal = 0, const QObject *receiver = 0, const char *member = 0) const { return disconnect(this, signal, receiver, member); } inline bool disconnect(const QObject *receiver, const char *member = 0) const { return disconnect(this, 0, receiver, member); } static bool disconnect(const QMetaObject::Connection &); #ifdef Q_QDOC static bool disconnect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method); #else template static inline bool disconnect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer::Object *receiver, Func2 slot) { typedef QtPrivate::FunctionPointer SignalType; typedef QtPrivate::FunctionPointer SlotType; Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value, "No Q_OBJECT in the class with the signal"); //compilation error if the arguments does not match. Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments::value), "Signal and slot arguments are not compatible."); return disconnectImpl(sender, reinterpret_cast(&signal), receiver, reinterpret_cast(&slot), &SignalType::Object::staticMetaObject); } template static inline bool disconnect(const typename QtPrivate::FunctionPointer::Object *sender, Func1 signal, const QObject *receiver, void **zero) { // This is the overload for when one wish to disconnect a signal from any slot. (slot=0) // Since the function template parameter cannot be deduced from '0', we use a // dummy void ** parameter that must be equal to 0 Q_ASSERT(!zero); typedef QtPrivate::FunctionPointer SignalType; return disconnectImpl(sender, reinterpret_cast(&signal), receiver, zero, &SignalType::Object::staticMetaObject); } #endif //Q_QDOC void dumpObjectTree(); void dumpObjectInfo(); #ifndef QT_NO_PROPERTIES bool setProperty(const char *name, const QVariant &value); QVariant property(const char *name) const; QList dynamicPropertyNames() const; #endif // QT_NO_PROPERTIES #ifndef QT_NO_USERDATA static uint registerUserData(); void setUserData(uint id, QObjectUserData* data); QObjectUserData* userData(uint id) const; #endif // QT_NO_USERDATA Q_SIGNALS: void destroyed(QObject * = 0); void objectNameChanged(const QString &objectName #if !defined(Q_QDOC) , QPrivateSignal #endif ); public: inline QObject *parent() const { return d_ptr->parent; } inline bool inherits(const char *classname) const { return const_cast(this)->qt_metacast(classname) != 0; } public Q_SLOTS: void deleteLater(); protected: QObject *sender() const; int senderSignalIndex() const; int receivers(const char* signal) const; bool isSignalConnected(const QMetaMethod &signal) const; virtual void timerEvent(QTimerEvent *); virtual void childEvent(QChildEvent *); virtual void customEvent(QEvent *); virtual void connectNotify(const QMetaMethod &signal); virtual void disconnectNotify(const QMetaMethod &signal); protected: QObject(QObjectPrivate &dd, QObject *parent = 0); protected: QScopedPointer d_ptr; static const QMetaObject staticQtMetaObject; friend inline const QMetaObject *qt_getQtMetaObject() Q_DECL_NOEXCEPT { return &staticQtMetaObject; } friend struct QMetaObject; friend struct QMetaObjectPrivate; friend class QMetaCallEvent; friend class QApplication; friend class QApplicationPrivate; friend class QCoreApplication; friend class QCoreApplicationPrivate; friend class QWidget; friend class QThreadData; private: Q_DISABLE_COPY(QObject) Q_PRIVATE_SLOT(d_func(), void _q_reregisterTimers(void *)) private: static QMetaObject::Connection connectImpl(const QObject *sender, void **signal, const QObject *receiver, void **slotPtr, QtPrivate::QSlotObjectBase *slot, Qt::ConnectionType type, const int *types, const QMetaObject *senderMetaObject); static bool disconnectImpl(const QObject *sender, void **signal, const QObject *receiver, void **slot, const QMetaObject *senderMetaObject); }; inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, const char *amember, Qt::ConnectionType atype) const { return connect(asender, asignal, this, amember, atype); } #ifndef QT_NO_USERDATA class Q_CORE_EXPORT QObjectUserData { public: virtual ~QObjectUserData(); }; #endif #ifdef Q_QDOC T qFindChild(const QObject *o, const QString &name = QString()); QList qFindChildren(const QObject *oobj, const QString &name = QString()); QList qFindChildren(const QObject *o, const QRegExp &re); #endif #if QT_DEPRECATED_SINCE(5, 0) template inline QT_DEPRECATED T qFindChild(const QObject *o, const QString &name = QString()) { return o->findChild(name); } template inline QT_DEPRECATED QList qFindChildren(const QObject *o, const QString &name = QString()) { return o->findChildren(name); } #ifndef QT_NO_REGEXP template inline QT_DEPRECATED QList qFindChildren(const QObject *o, const QRegExp &re) { return o->findChildren(re); } #endif #endif //QT_DEPRECATED template inline T qobject_cast(QObject *object) { typedef typename QtPrivate::remove_cv::type>::type ObjType; Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value, "qobject_cast requires the type to have a Q_OBJECT macro"); return static_cast(ObjType::staticMetaObject.cast(object)); } template inline T qobject_cast(const QObject *object) { typedef typename QtPrivate::remove_cv::type>::type ObjType; Q_STATIC_ASSERT_X(QtPrivate::HasQ_OBJECT_Macro::Value, "qobject_cast requires the type to have a Q_OBJECT macro"); return static_cast(ObjType::staticMetaObject.cast(object)); } template inline const char * qobject_interface_iid() { return 0; } #ifndef Q_MOC_RUN # define Q_DECLARE_INTERFACE(IFace, IId) \ template <> inline const char *qobject_interface_iid() \ { return IId; } \ template <> inline IFace *qobject_cast(QObject *object) \ { return reinterpret_cast((object ? object->qt_metacast(IId) : 0)); } \ template <> inline IFace *qobject_cast(const QObject *object) \ { return reinterpret_cast((object ? const_cast(object)->qt_metacast(IId) : 0)); } #endif // Q_MOC_RUN #ifndef QT_NO_DEBUG_STREAM Q_CORE_EXPORT QDebug operator<<(QDebug, const QObject *); #endif class QSignalBlocker { public: inline explicit QSignalBlocker(QObject *o); inline explicit QSignalBlocker(QObject &o); inline ~QSignalBlocker(); #ifdef Q_COMPILER_RVALUE_REFS inline QSignalBlocker(QSignalBlocker &&other); inline QSignalBlocker &operator=(QSignalBlocker &&other); #endif inline void reblock(); inline void unblock(); private: Q_DISABLE_COPY(QSignalBlocker) QObject * m_o; bool m_blocked; bool m_inhibited; }; QSignalBlocker::QSignalBlocker(QObject *o) : m_o(o), m_blocked(o && o->blockSignals(true)), m_inhibited(false) {} QSignalBlocker::QSignalBlocker(QObject &o) : m_o(&o), m_blocked(o.blockSignals(true)), m_inhibited(false) {} #ifdef Q_COMPILER_RVALUE_REFS QSignalBlocker::QSignalBlocker(QSignalBlocker &&other) : m_o(other.m_o), m_blocked(other.m_blocked), m_inhibited(other.m_inhibited) { other.m_o = 0; } QSignalBlocker &QSignalBlocker::operator=(QSignalBlocker &&other) { if (this != &other) { // if both *this and other block the same object's signals: // unblock *this iff our dtor would unblock, but other's wouldn't if (m_o != other.m_o || (!m_inhibited && other.m_inhibited)) unblock(); m_o = other.m_o; m_blocked = other.m_blocked; m_inhibited = other.m_inhibited; // disable other: other.m_o = 0; } return *this; } #endif QSignalBlocker::~QSignalBlocker() { if (m_o && !m_inhibited) m_o->blockSignals(m_blocked); } void QSignalBlocker::reblock() { if (m_o) m_o->blockSignals(true); m_inhibited = false; } void QSignalBlocker::unblock() { if (m_o) m_o->blockSignals(m_blocked); m_inhibited = true; } namespace QtPrivate { inline QObject & deref_for_methodcall(QObject &o) { return o; } inline QObject & deref_for_methodcall(QObject *o) { return *o; } } #define Q_SET_OBJECT_NAME(obj) QT_PREPEND_NAMESPACE(QtPrivate)::deref_for_methodcall(obj).setObjectName(QLatin1String(#obj)) QT_END_NAMESPACE #endif #endif // QOBJECT_H