📄 qobject.cpp
字号:
/*! \internal */QMetaCallEvent::QMetaCallEvent(int id, const QObject *sender, int nargs, int *types, void **args) :QEvent(MetaCall), id_(id), sender_(sender), nargs_(nargs), types_(types), args_(args){ }/*! \internal */QMetaCallEvent::~QMetaCallEvent(){ for (int i = 0; i < nargs_; ++i) { if (types_[i] && args_[i]) QMetaType::destroy(types_[i], args_[i]); } if (types_) qFree(types_); if (args_) qFree(args_);}/*! \class QObject \brief The QObject class is the base class of all Qt objects. \ingroup objectmodel \mainclass \reentrant QObject is the heart of the \l{Qt object model}. The central feature in this model is a very powerful mechanism for seamless object communication called \l{signals and slots}. You can connect a signal to a slot with connect() and destroy the connection with disconnect(). To avoid never ending notification loops you can temporarily block signals with blockSignals(). The protected functions connectNotify() and disconnectNotify() make it possible to track connections. QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object i.e. it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren(). Every object has an objectName() and its class name can be found via the corresponding metaObject() (see QMetaObject::className()). You can determine whether the object's class inherits another class in the QObject inheritance hierarchy by using the inherits() function. When an object is deleted, it emits a destroyed() signal. You can catch this signal to avoid dangling references to QObjects. The QPointer class provides an elegant way to use this feature. QObjects can receive events through event() and filter the events of other objects. See installEventFilter() and eventFilter() for details. A convenience handler, childEvent(), can be reimplemented to catch child events. Events are delivered in the thread in which the object was created; see \l{Thread Support in Qt} and thread() for details. Note that for QObjects that are created before QApplication, thread() returns zero. This means that the main thread will only handle posted events for these objects; other event processing is not done at all for objects with no thread. Use the moveToThread() function to change the thread affinity for an object and its children (the object cannot be moved if it has a parent). Last but not least, QObject provides the basic timer support in Qt; see QTimer for high-level support for timers. Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the \l{moc}{Meta Object Compiler} on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior. All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than \l{qobject_cast()}{qobject_cast}<QWidget>(\e{obj}) or \e{obj}->\l{inherits()}{inherits}("QWidget"). Some QObject functions, e.g. children(), return a QObjectList. QObjectList is a typedef for QList<QObject *>. \sa QMetaObject, QPointer, QObjectCleanupHandler, {Object Trees and Object Ownership}*//*! \relates QObject Returns a pointer to the object named \a name that inherits \a type and with a given \a parent. Returns 0 if there is no such child. \code QLineEdit *lineEdit = static_cast<QLineEdit *>( qt_find_obj_child(myWidget, "QLineEdit", "my line edit")); if (lineEdit) lineEdit->setText("Default"); \endcode*/void *qt_find_obj_child(QObject *parent, const char *type, const QString &name){ QObjectList list = parent->children(); if (list.size() == 0) return 0; for (int i = 0; i < list.size(); ++i) { QObject *obj = list.at(i); if (name == obj->objectName() && obj->inherits(type)) return obj; } return 0;}/***************************************************************************** QObject member functions *****************************************************************************//*! Constructs an object with parent object \a parent. The parent of an object may be viewed as the object's owner. For instance, a \l{QDialog}{dialog box} is the parent of the \gui OK and \gui Cancel buttons it contains. The destructor of a parent object destroys all child objects. Setting \a parent to 0 constructs an object with no parent. If the object is a widget, it will become a top-level window. \sa parent(), findChild(), findChildren()*/QObject::QObject(QObject *parent) : d_ptr(new QObjectPrivate){ Q_D(QObject); ::qt_addObject(d_ptr->q_ptr = this); QThread *currentThread = QThread::currentThread(); d->thread = currentThread ? QThreadData::get(currentThread)->id : -1; Q_ASSERT_X(!parent || parent->d_func()->thread == d->thread, "QObject::QObject()", "Cannot create children for a parent that is in a different thread."); if (parent && parent->d_func()->thread != d->thread) parent = 0; setParent(parent);}#ifdef QT3_SUPPORT/*! \overload \obsolete Creates a new QObject with the given \a parent and object \a name. */QObject::QObject(QObject *parent, const char *name) : d_ptr(new QObjectPrivate){ Q_D(QObject); ::qt_addObject(d_ptr->q_ptr = this); QThread *currentThread = QThread::currentThread(); d->thread = currentThread ? QThreadData::get(currentThread)->id : -1; Q_ASSERT_X(!parent || parent->d_func()->thread == d->thread, "QObject::QObject()", "Cannot create children for a parent that is in a different thread."); if (parent && parent->d_func()->thread != d->thread) parent = 0; setParent(parent); setObjectName(QString::fromAscii(name));}#endif/*! \internal */QObject::QObject(QObjectPrivate &dd, QObject *parent) : d_ptr(&dd){ Q_D(QObject); ::qt_addObject(d_ptr->q_ptr = this); QThread *currentThread = QThread::currentThread(); d->thread = currentThread ? QThreadData::get(currentThread)->id : -1; Q_ASSERT_X(!parent || parent->d_func()->thread == d->thread, "QObject::QObject()", "Cannot create children for a parent that is in a different thread."); if (parent && parent->d_func()->thread != d->thread) parent = 0; if (d->isWidget) { if (parent) { d->parent = parent; d->parent->d_func()->children.append(this); } // no events sent here, this is done at the end of the QWidget constructor } else { setParent(parent); }}/*! Destroys the object, deleting all its child objects. All signals to and from the object are automatically disconnected. \warning All child objects are deleted. If any of these objects are on the stack or global, sooner or later your program will crash. We do not recommend holding pointers to child objects from outside the parent. If you still do, the destroyed() signal gives you an opportunity to detect when an object is destroyed. \warning Deleting a QObject while pending events are waiting to be delivered can cause a crash. You must not delete the QObject directly if it exists in a different thread than the one currently executing. Use the deleteLater() method instead, which will cause the event loop to delete the object after all pending events have been delivered to it. \sa deleteLater()*/QObject::~QObject(){ Q_D(QObject); if (d->wasDeleted) {#if defined(QT_DEBUG) qWarning("Double QObject deletion detected");#endif return; } d->wasDeleted = true; d->blockSig = 0; // unblock signals so we always emit destroyed() // set all QPointers for this object to zero GuardHash *hash = ::guardHash(); if (hash) { QWriteLocker locker(guardHashLock()); GuardHash::iterator it = hash->find(this); const GuardHash::iterator end = hash->end(); while (it.key() == this && it != end) { *it.value() = 0; it = hash->erase(it); } } emit destroyed(this); QConnectionList *list = ::connectionList(); if (list) { QWriteLocker locker(&list->lock); list->remove(this); } if (d->pendTimer) { // have pending timers QThread *thr = thread(); if (thr || d->thread == 0) { // don't unregister timers in the wrong thread QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(thr); if (eventDispatcher) eventDispatcher->unregisterTimers(this); } } d->eventFilters.clear(); // delete children objects if (!d->children.isEmpty()) { qDeleteAll(d->children); d->children.clear(); } { QWriteLocker locker(QObjectPrivate::readWriteLock()); ::qt_removeObject(this); /* 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 noone 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); 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.*//*! \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. 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. \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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -