📄 qobject.cpp
字号:
#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; 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; d->currentSender = const_cast<QObject*>(mce->sender());#if defined(QT_NO_EXCEPTIONS) qt_metacall(QMetaObject::InvokeMetaMethod, mce->id(), mce->args());#else try { qt_metacall(QMetaObject::InvokeMetaMethod, mce->id(), mce->args()); } catch (...) { QReadLocker locker(QObjectPrivate::readWriteLock()); if (QObjectPrivate::isValidObject(this)) d->currentSender = previousSender; throw; }#endif QReadLocker locker(QObjectPrivate::readWriteLock()); if (QObjectPrivate::isValidObject(this)) d->currentSender = previousSender; break; } case QEvent::ThreadChange: { QThread *objectThread = thread(); if (objectThread) { QThreadData *threadData = QThreadData::get(objectThread); QAbstractEventDispatcher *eventDispatcher = threadData->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. Example: \code class MainWindow : public QMainWindow { public: MainWindow(); protected: bool eventFilter(QObject *obj, QEvent *ev); private: QTextEdit *textEdit; }; MainWindow::MainWindow() { textEdit = new QTextEdit; setCentralWidget(textEdit); textEdit->installEventFilter(this); } bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if (obj == textEdit) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); qDebug() << "Ate key press" << keyEvent->key(); return true; } else { return false; } } else { // pass the event on to the parent class return QMainWindow::eventFilter(obj, event); } } \endcode Notice in the example above that unhandled events are passed to the base class's eventFilter() function, since the base class might have reimplemented eventFilter() for its own internal purposes. \warning If you delete the receiver object in this function, be sure to return true. Otherwise, Qt will forward the event to the deleted object and the program might crash. \sa installEventFilter()*/bool QObject::eventFilter(QObject * /* watched */, QEvent * /* event */){ return false;}/*! \fn bool QObject::signalsBlocked() const Returns true if signals are blocked; otherwise returns false. Signals are not blocked by default. \sa blockSignals()*//*! Blocks signals if \a block is true, or unblocks signals if \a block is false. Emitted signals disappear into hyperspace if signals are blocked. Note that the destroyed() signals will be emitted even if the signals for this object have been blocked. \sa signalsBlocked()*/bool QObject::blockSignals(bool block){ Q_D(QObject); bool previous = d->blockSig; d->blockSig = block; return previous;}/*! Returns the thread in which the object lives. \warning This function returns 0 if the QObject was created before QApplication or QCoreApplication was constructed. This behavior might change in future versions of Qt. \sa moveToThread()*/QThread *QObject::thread() const{ return QThreadPrivate::threadForId(d_func()->thread); }/*! Changes the thread affinity for this object and its children. The object cannot be moved if it has a parent. Event processing will continue in the \a targetThread. To move an object to the main thread, pass QCoreApplication::thread() as the \a targetThread. If \a targetThread is zero, only \l{QCoreApplication::postEvent()}{posted events} are processed by the main thread; all other event processing for this object and its children stops. Note that all active timers for the object will be reset. The timers are first stopped in the current thread and restarted (with the same interval) in the \a targetThread. As a result, constantly moving an object between threads can postpone timer events indefinitely. \warning This function is \e not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread. \sa thread() */void QObject::moveToThread(QThread *targetThread){ Q_D(QObject); Q_ASSERT_X(d->parent == 0, "QObject::moveToThread", "Cannot move objects with a parent"); if (d->parent != 0) return; Q_ASSERT_X(!d->isWidget, "QObject::moveToThread", "Widgets cannot be moved to a new thread"); if (d->isWidget) return; QThread *objectThread = thread(); QThread *currentThread = QThread::currentThread(); Q_ASSERT_X(d->thread == -1 || objectThread == currentThread, "QObject::moveToThread", "Current thread is not the object's thread"); if (d->thread != -1 && objectThread != currentThread) return; if (objectThread == targetThread) return; d->moveToThread_helper(targetThread); QWriteLocker locker(QObjectPrivate::readWriteLock()); QThreadData *currentData = QThreadData::get(currentThread); QThreadData *targetData = QThreadData::get(targetThread ? targetThread : QCoreApplicationPrivate::mainThread()); if (currentData != targetData) { targetData->postEventList.mutex.lock(); while (!currentData->postEventList.mutex.tryLock()) { targetData->postEventList.mutex.unlock(); targetData->postEventList.mutex.lock(); } } d_func()->setThreadId_helper(currentData, targetData, targetThread ? QThreadData::get(targetThread)->id : -1); if (currentData != targetData) { targetData->postEventList.mutex.unlock(); currentData->postEventList.mutex.unlock(); }}void QObjectPrivate::moveToThread_helper(QThread *targetThread){ Q_Q(QObject); QEvent e(QEvent::ThreadChange); QCoreApplication::sendEvent(q, &e); for (int i = 0; i < children.size(); ++i) { QObject *child = children.at(i); child->d_func()->moveToThread_helper(targetThread); }}void QObjectPrivate::setThreadId_helper(QThreadData *currentData, QThreadData *targetData, int newThreadId){ Q_Q(QObject); if (currentData != targetData) { // move posted events int eventsMoved = 0; for (int i = 0; i < currentData->postEventList.size(); ++i) { const QPostEvent &pe = currentData->postEventList.at(i); if (!pe.event) continue; if (pe.receiver == q) { // move this post event to the targetList targetData->postEventList.append(pe); const_cast<QPostEvent &>(pe).event = 0; ++eventsMoved; } } if (eventsMoved > 0 && targetData->eventDispatcher) targetData->eventDispatcher->wakeUp();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -