📄 qobject.cpp
字号:
and returns a list of those objects that are named or that match \a objName and inherit \a inheritsClass. If \a inheritsClass is 0 (the default), all classes match. If \a objName is 0 (the default), all object names match. If \a regexpMatch is true (the default), \a objName is a regular expression that the objects's names must match. The syntax is that of a QRegExp. If \a regexpMatch is false, \a objName is a string and object names must match it exactly. Note that \a inheritsClass uses single inheritance from QObject, the way inherits() does. According to inherits(), QWidget inherits QObject but not QPaintDevice. This does not quite match reality, but is the best that can be done on the wide variety of compilers Qt supports. Finally, if \a recursiveSearch is true (the default), queryList() searches \e{n}th-generation as well as first-generation children. If all this seems a bit complex for your needs, the simpler child() function may be what you want. This somewhat contrived example disables all the buttons in this window: \code QList<QObject *> list = window()->queryList("QAbstractButton")); foreach (QObject *obj, list) static_cast<QAbstractButton *>(obj)->setEnabled(false); \endcode \warning Delete the list as soon you have finished using it. The list contains pointers that may become invalid at almost any time without notice (as soon as the user closes a window you may have dangling pointers, for example). \sa child() children(), parent(), inherits(), objectName(), QRegExp*/QObjectList QObject::queryList(const char *inheritsClass, const char *objName, bool regexpMatch, bool recursiveSearch) const{ Q_D(const QObject); QObjectList list; bool onlyWidgets = (inheritsClass && qstrcmp(inheritsClass, "QWidget") == 0);#ifndef QT_NO_REGEXP if (regexpMatch && objName) { // regexp matching QRegExp rx(QString::fromLatin1(objName)); objSearch(list, d->children, inheritsClass, onlyWidgets, 0, &rx, recursiveSearch); } else#endif { objSearch(list, d->children, inheritsClass, onlyWidgets, objName, 0, recursiveSearch); } return list;}#endif/*! \fn T *QObject::findChild(const QString &name) const Returns the child of this object that can be cast into type T and that is called \a name, or 0 if there is no such object. Omitting the \a name argument causes all object names to be matched. The search is performed recursively. If there is more than one child matching the search, the most direct ancestor is returned. If there are several direct ancestors, it is undefined which one will be returned. In that case, findChildren() should be used. This example returns a child \l{QPushButton} of \c{parentWidget} named \c{"button1"}: \code QPushButton *button = parentWidget->findChild<QPushButton *>("button1"); \endcode This example returns a \l{QListWidget} child of \c{parentWidget}: \code QListWidget *list = parentWidget->findChild<QListWidget *>(); \endcode \warning This function is not available with MSVC 6. Use qFindChild() instead if you need to support that version of the compiler. \sa findChildren(), qFindChild()*//*! \fn QList<T> QObject::findChildren(const QString &name) const Returns all children of this object with the given \a name that can be cast to type T, or an empty list if there are no such objects. Omitting the \a name argument causes all object names to be matched. The search is performed recursively. The following example shows how to find a list of child \l{QWidget}s of the specified \c{parentWidget} named \c{widgetname}: \code QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname"); \endcode This example returns all \c{QPushButton}s that are children of \c{parentWidget}: \code QList<QPushButton *> allPButtons = parentWidget.findChildren<QPushButton *>(); \endcode \warning This function is not available with MSVC 6. Use qFindChildren() instead if you need to support that version of the compiler. \sa findChild(), qFindChildren()*//*! \fn QList<T> QObject::findChildren(const QRegExp ®Exp) const \overload Returns the children of this object that can be cast to type T and that have names matching the regular expression \a regExp, or an empty list if there are no such objects. The search is performed recursively. \warning This function is not available with MSVC 6. Use qFindChildren() instead if you need to support that version of the compiler.*//*! \fn T qFindChild(const QObject *obj, const QString &name) \relates QObject This function is equivalent to \a{obj}->\l{QObject::findChild()}{findChild}<T>(\a name). It is provided as a work-around for MSVC 6, which doesn't support member template functions. \sa QObject::findChild()*//*! \fn QList<T> qFindChildren(const QObject *obj, const QString &name) \relates QObject This function is equivalent to \a{obj}->\l{QObject::findChildren()}{findChildren}<T>(\a name). It is provided as a work-around for MSVC 6, which doesn't support member template functions. \sa QObject::findChildren()*//*! \fn QList<T> qFindChildren(const QObject *obj, const QRegExp ®Exp) \relates QObject \overload This function is equivalent to \a{obj}->\l{QObject::findChildren()}{findChildren}<T>(\a regExp). It is provided as a work-around for MSVC 6, which doesn't support member template functions.*//*! \internal \fn T qFindChild(const QObject *obj, const QString &name = QString(), T dummy = 0) \relates QObject \overload This function is equivalent to \a{obj}->\l{QObject::findChild()}{findChild}<T>(\a name). It is provided as a work-around for MSVC 6, which doesn't support member template functions. \sa QObject::findChild()*//*! \internal \fn QList<T> qFindChildren(const QObject *obj, const QString &name = QString(), T dummy = 0) \relates QObject \overload This function is equivalent to \a{obj}->\l{QObject::findChildren()}{findChildren}<T>(\a name). It is provided as a work-around for MSVC 6, which doesn't support member template functions. \sa QObject::findChildren()*//*! \internal*/void qt_qFindChildren_helper(const QObject *parent, const QString &name, const QRegExp *re, const QMetaObject &mo, QList<void*> *list){ if (!parent || !list) return; const QObjectList &children = parent->children(); QObject *obj; for (int i = 0; i < children.size(); ++i) { obj = children.at(i); if (mo.cast(obj)) { if (re) { if (re->indexIn(obj->objectName()) != -1) list->append(obj); } else { if (name.isNull() || obj->objectName() == name) list->append(obj); } } qt_qFindChildren_helper(obj, name, re, mo, list); }}/*! \internal */QObject *qt_qFindChild_helper(const QObject *parent, const QString &name, const QMetaObject &mo){ if (!parent) return 0; const QObjectList &children = parent->children(); QObject *obj; int i; for (i = 0; i < children.size(); ++i) { obj = children.at(i); if (mo.cast(obj) && (name.isNull() || obj->objectName() == name)) return obj; } for (i = 0; i < children.size(); ++i) { obj = qt_qFindChild_helper(children.at(i), name, mo); if (obj) return obj; } return 0;}/*! Makes the object a child of \a parent. \sa QWidget::setParent()*/void QObject::setParent(QObject *parent){ Q_D(QObject); Q_ASSERT(!d->isWidget); d->setParent_helper(parent);}void QObjectPrivate::deleteChildren(){ const bool reallyWasDeleted = wasDeleted; wasDeleted = true; // delete children objects // don't use qDeleteAll as the destructor of the child might // delete siblings for (int i = 0; i < children.count(); ++i) { currentChildBeingDeleted = children.at(i); children[i] = 0; delete currentChildBeingDeleted; } children.clear(); currentChildBeingDeleted = 0; wasDeleted = reallyWasDeleted;}void QObjectPrivate::setParent_helper(QObject *o){ Q_Q(QObject); if (o == parent) return; if (parent) { QObjectPrivate *parentD = parent->d_func(); if (parentD->wasDeleted && wasDeleted && parentD->currentChildBeingDeleted == q) { // don't do anything since QObjectPrivate::deleteChildren() already // cleared our entry in parentD->children. } else { const int index = parentD->children.indexOf(q); if (parentD->wasDeleted) { parentD->children[index] = 0; } else { parentD->children.removeAt(index); if (sendChildEvents && parentD->receiveChildEvents) { QChildEvent e(QEvent::ChildRemoved, q); QCoreApplication::sendEvent(parent, &e); } } } } parent = o; if (parent) { // object hierarchies are constrained to a single thread if (threadData != parent->d_func()->threadData) { qWarning("QObject::setParent: Cannot set parent, new parent is in a different thread"); parent = 0; return; } parent->d_func()->children.append(q); if(sendChildEvents && parent->d_func()->receiveChildEvents) { if (!isWidget) { QChildEvent e(QEvent::ChildAdded, q); QCoreApplication::sendEvent(parent, &e);#ifdef QT3_SUPPORT if (parent->d_func()->pendingChildInsertedEvents.isEmpty()) { QCoreApplication::postEvent(parent, new QEvent(QEvent::ChildInsertedRequest), Qt::HighEventPriority); } parent->d_func()->pendingChildInsertedEvents.append(q);#endif } } }}/*! \fn void QObject::installEventFilter(QObject *filterObj) \threadsafe Installs an event filter \a filterObj on this object. For example: \code monitoredObj->installEventFilter(filterObj); \endcode An event filter is an object that receives all events that are sent to this object. The filter can either stop the event or forward it to this object. The event filter \a filterObj receives events via its eventFilter() function. The eventFilter() function must return true if the event should be filtered, (i.e. stopped); otherwise it must return false. If multiple event filters are installed on a single object, the filter that was installed last is activated first. Here's a \c KeyPressEater class that eats the key presses of its monitored objects: \code class KeyPressEater : public QObject { Q_OBJECT ... protected: bool eventFilter(QObject *obj, QEvent *event); }; bool KeyPressEater::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("Ate key press %d", keyEvent->key()); return true; } else { // standard event processing return QObject::eventFilter(obj, event); } } \endcode And here's how to install it on two widgets: \code KeyPressEater *keyPressEater = new KeyPressEater(this); QPushButton *pushButton = new QPushButton(this); QListView *listView = new QListView(this); pushButton->installEventFilter(keyPressEater); listView->installEventFilter(keyPressEater); \endcode The QShortcut class, for example, uses this technique to intercept shortcut key presses. \warning If you delete the receiver object in your eventFilter() function, be sure to return true. If you return false, Qt sends the event to the deleted object and the program will crash. \sa removeEventFilter(), eventFilter(), event()*/void QObject::installEventFilter(QObject *obj){ Q_D(QObject); if (!obj) return; QWriteLocker locker(QObjectPrivate::readWriteLock()); // clean up unused items in the list d->eventFilters.removeAll((QObject*)0); d->eventFilters.removeAll(obj); d->eventFilters.prepend(obj);}/*! \threadsafe Removes an event filter object \a obj from this object. The request is ignored if such an event filter has not been installed. All event filters for this object are automatically removed when t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -