📄 qobject.cpp
字号:
/* theoretically, we cannot check d->postedEvents without holding the postEventList.mutex for the object's thread, but since we hold the QObjectPrivate::readWriteLock(), nothing can go into QCoreApplication::postEvent(), which effectively means no one can post new events, which is what we are trying to prevent. this means we can safely check d->postedEvents, since we are fairly sure it will not change (it could, but only by decreasing, i.e. removing posted events from a differebnt thread) */ if (d->postedEvents > 0) QCoreApplication::removePostedEvents(this); } if (d->parent) // remove it from parent object d->setParent_helper(0); d->threadData->deref(); delete d; d_ptr = 0;}/*! \fn QMetaObject *QObject::metaObject() const Returns a pointer to the meta-object of this object. A meta-object contains information about a class that inherits QObject, e.g. class name, superclass name, properties, signals and slots. Every class that contains the Q_OBJECT macro will also have a meta-object. The meta-object information is required by the signal/slot connection mechanism and the property system. The inherits() function also makes use of the meta-object. If you have no pointer to an actual object instance but still want to access the meta-object of a class, you can use \l staticMetaObject. Example: \code QObject *obj = new QPushButton; obj->metaObject()->className(); // returns "QPushButton" QPushButton::staticMetaObject.className(); // returns "QPushButton" \endcode \sa staticMetaObject*//*! \variable QObject::staticMetaObject This variable stores the meta-object for the class. A meta-object contains information about a class that inherits QObject, e.g. class name, superclass name, properties, signals and slots. Every class that contains the Q_OBJECT macro will also have a meta-object. The meta-object information is required by the signal/slot connection mechanism and the property system. The inherits() function also makes use of the meta-object. If you have a pointer to an object, you can use metaObject() to retrieve the meta-object associated with that object. Example: \code QPushButton::staticMetaObject.className(); // returns "QPushButton" QObject *obj = new QPushButton; obj->metaObject()->className(); // returns "QPushButton" \endcode \sa metaObject()*//*! \fn T *qobject_cast<T *>(QObject *object) \relates QObject Returns the given \a object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. The class T must inherit (directly or indirectly) QObject and be declared with the \l Q_OBJECT macro. A class is considered to inherit itself. Example: \code QObject *obj = new QTimer; // QTimer inherits QObject QTimer *timer = qobject_cast<QTimer *>(obj); // timer == (QObject *)obj QAbstractButton *button = qobject_cast<QAbstractButton *>(obj); // button == 0 \endcode The qobject_cast() function behaves similarly to the standard C++ \c dynamic_cast(), with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries. qobject_cast() can also be used in conjunction with interfaces; see the \l{tools/plugandpaint}{Plug & Paint} example for details. \warning If T isn't declared with the Q_OBJECT macro, this function's return value is undefined. \sa QObject::inherits()*//*! \fn bool QObject::inherits(const char *className) const Returns true if this object is an instance of a class that inherits \a className or a QObject subclass that inherits \a className; otherwise returns false. A class is considered to inherit itself. Example: \code QTimer *timer = new QTimer; // QTimer inherits QObject timer->inherits("QTimer"); // returns true timer->inherits("QObject"); // returns true timer->inherits("QAbstractButton"); // returns false // QLayout inherits QObject and QLayoutItem QLayout *layout = new QLayout; layout->inherits("QObject"); // returns true layout->inherits("QLayoutItem"); // returns false \endcode (\l QLayoutItem is not a QObject.) Consider using qobject_cast<Type *>(object) instead. The method is both faster and safer. \sa metaObject(), qobject_cast()*//*! \property QObject::objectName \brief the name of this object You can find an object by name (and type) using findChild(). You can find a set of objects with findChildren(). \code qDebug("MyClass::setPrecision(): (%s) invalid precision %f", qPrintable(objectName()), newPrecision); \endcode \sa metaObject(), QMetaObject::className()*/QString QObject::objectName() const{ Q_D(const QObject); return d->objectName;}/* Sets the object's name to \a name.*/void QObject::setObjectName(const QString &name){ Q_D(QObject); d->objectName = name;}#ifdef QT3_SUPPORT/*! \internal QObject::child is compat but needs to call itself recursively, that's why we need this helper.*/static QObject *qChildHelper(const char *objName, const char *inheritsClass, bool recursiveSearch, const QObjectList &children){ if (children.isEmpty()) return 0; bool onlyWidgets = (inheritsClass && qstrcmp(inheritsClass, "QWidget") == 0); const QLatin1String oName(objName); for (int i = 0; i < children.size(); ++i) { QObject *obj = children.at(i); if (onlyWidgets) { if (obj->isWidgetType() && (!objName || obj->objectName() == oName)) return obj; } else if ((!inheritsClass || obj->inherits(inheritsClass)) && (!objName || obj->objectName() == oName)) return obj; if (recursiveSearch && (obj = qChildHelper(objName, inheritsClass, recursiveSearch, obj->children()))) return obj; } return 0;}/*! Searches the children and optionally grandchildren of this object, and returns a child that is called \a objName that inherits \a inheritsClass. If \a inheritsClass is 0 (the default), any class matches. If \a recursiveSearch is true (the default), child() performs a depth-first search of the object's children. If there is no such object, this function returns 0. If there are more than one, the first one found is returned.*/QObject* QObject::child(const char *objName, const char *inheritsClass, bool recursiveSearch) const{ Q_D(const QObject); return qChildHelper(objName, inheritsClass, recursiveSearch, d->children);}#endif/*! \fn bool QObject::isWidgetType() const Returns true if the object is a widget; otherwise returns false. Calling this function is equivalent to calling inherits("QWidget"), except that it is much faster.*//*! This virtual function receives events to an object and should return true if the event \a e was recognized and processed. The event() function can be reimplemented to customize the behavior of an object. \sa installEventFilter(), timerEvent(), QApplication::sendEvent(), QApplication::postEvent(), QWidget::event()*/bool QObject::event(QEvent *e){ switch (e->type()) { case QEvent::Timer: timerEvent((QTimerEvent*)e); break;#ifdef QT3_SUPPORT case QEvent::ChildInsertedRequest: d_func()->sendPendingChildInsertedEvents(); break;#endif case QEvent::ChildAdded: case QEvent::ChildPolished:#ifdef QT3_SUPPORT case QEvent::ChildInserted:#endif case QEvent::ChildRemoved: childEvent((QChildEvent*)e); break; case QEvent::DeferredDelete: delete this; break; case QEvent::MetaCall: { Q_D(QObject); QMetaCallEvent *mce = static_cast<QMetaCallEvent*>(e); QObject *previousSender = d->currentSender; int previousFrom = d->currentSenderSignalIdStart; int previousTo = d->currentSenderSignalIdEnd; d->currentSender = const_cast<QObject*>(mce->sender()); d->currentSenderSignalIdStart = mce->signalIdStart(); d->currentSenderSignalIdEnd = mce->signalIdEnd();#if defined(QT_NO_EXCEPTIONS) mce->placeMetaCall(this);#else try { mce->placeMetaCall(this); } catch (...) { QReadLocker locker(QObjectPrivate::readWriteLock()); if (QObjectPrivate::isValidObject(this)) { d->currentSender = previousSender; d->currentSenderSignalIdStart = previousFrom; d->currentSenderSignalIdEnd = previousTo; } throw; }#endif QReadLocker locker(QObjectPrivate::readWriteLock()); if (QObjectPrivate::isValidObject(this)) { d->currentSender = previousSender; d->currentSenderSignalIdStart = previousFrom; d->currentSenderSignalIdEnd = previousTo; } break; } case QEvent::ThreadChange: { QThreadData *threadData = d_func()->threadData; QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher; if (eventDispatcher) { QList<QPair<int, int> > timers = eventDispatcher->registeredTimers(this); if (!timers.isEmpty()) { eventDispatcher->unregisterTimers(this); QMetaObject::invokeMethod(this, "_q_reregisterTimers", Qt::QueuedConnection, Q_ARG(void*, (new QList<QPair<int, int> >(timers)))); } } break; } default: if (e->type() >= QEvent::User) { customEvent(e); break; } return false; } return true;}/*! \fn void QObject::timerEvent(QTimerEvent *event) This event handler can be reimplemented in a subclass to receive timer events for the object. QTimer provides a higher-level interface to the timer functionality, and also more general information about timers. The timer event is passed in the \a event parameter. \sa startTimer(), killTimer(), event()*/void QObject::timerEvent(QTimerEvent *){}/*! This event handler can be reimplemented in a subclass to receive child events. The event is passed in the \a event parameter. QEvent::ChildAdded and QEvent::ChildRemoved events are sent to objects when children are added or removed. In both cases you can only rely on the child being a QObject, or if isWidgetType() returns true, a QWidget. (This is because, in the \l{QEvent::ChildAdded}{ChildAdded} case, the child is not yet fully constructed, and in the \l{QEvent::ChildRemoved}{ChildRemoved} case it might have been destructed already). QEvent::ChildPolished events are sent to widgets when children are polished, or when polished children are added. If you receive a child polished event, the child's construction is usually completed. For every child widget, you receive one \l{QEvent::ChildAdded}{ChildAdded} event, zero or more \l{QEvent::ChildPolished}{ChildPolished} events, and one \l{QEvent::ChildRemoved}{ChildRemoved} event. The \l{QEvent::ChildPolished}{ChildPolished} event is omitted if a child is removed immediately after it is added. If a child is polished several times during construction and destruction, you may receive several child polished events for the same child, each time with a different virtual table. \sa event()*/void QObject::childEvent(QChildEvent * /* event */){}/*! This event handler can be reimplemented in a subclass to receive custom events. Custom events are user-defined events with a type value at least as large as the QEvent::User item of the QEvent::Type enum, and is typically a QEvent subclass. The event is passed in the \a event parameter. \sa event(), QEvent*/void QObject::customEvent(QEvent * /* event */){}/*! Filters events if this object has been installed as an event filter for the \a watched object. In your reimplementation of this function, if you want to filter the \a event out, i.e. stop it being handled further, return true; otherwise return false.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -