⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qobject.cpp

📁 QT 开发环境里面一个很重要的文件
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    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;    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)            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;                    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.    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()*//*!    If \a block is true, signals emitted by this object is blocked    (i.e., emitted signals disappear into hyperspace). If \a block is    false, no such blocking will occur.    The return value is the previous value of signalsBlocked().    Note that the destroyed() signal 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.    \sa moveToThread()*/QThread *QObject::thread() const{    return d_func()->threadData->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, use QApplication::instance()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -