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

📄 qobject.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    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 are 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()    to retrieve a pointer to the current application, and then use    QApplication::thread() to retrieve the thread in which the    application lives. For example:    \code        myObject->moveToThread(QApplication::instance()->thread());    \endcode    If \a targetThread is zero, all 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.    A QEvent::ThreadChange event is sent to this object just before    the thread affinity is changed. You can handle this event to    perform any special processing. Note that any new events that are    posted to this object will be handled in the \a targetThread.    \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);    if (d->threadData->thread == targetThread) {        // object is already in this thread        return;    }    if (d->parent != 0) {        qWarning("QObject::moveToThread: Cannot move objects with a parent");        return;    }    if (d->isWidget) {        qWarning("QObject::moveToThread: Widgets cannot be moved to a new thread");        return;    }    QThreadData *currentData = QThreadData::current();    QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : new QThreadData(0);    if (d->threadData->thread == 0 && currentData == targetData) {        // one exception to the rule: we allow moving objects with no thread affinity to the current thread        currentData = d->threadData;    } else if (d->threadData != currentData) {        qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n"                 "Cannot move to target thread (%p)\n",                 d->threadData->thread, currentData->thread, targetData->thread);        return;    }    // prepare to move    d->moveToThread_helper();    QWriteLocker locker(QObjectPrivate::readWriteLock());    if (currentData != targetData) {        targetData->postEventList.mutex.lock();        while (currentData && !currentData->postEventList.mutex.tryLock()) {            targetData->postEventList.mutex.unlock();            targetData->postEventList.mutex.lock();        }    }    // keep currentData alive (since we've got it locked)    currentData->ref();    // move the object    d_func()->setThreadData_helper(currentData, targetData);    if (currentData != targetData) {        targetData->postEventList.mutex.unlock();        if (currentData)            currentData->postEventList.mutex.unlock();    }    // now currentData can commit suicide if it wants to    currentData->deref();}void QObjectPrivate::moveToThread_helper(){    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();    }}void QObjectPrivate::setThreadData_helper(QThreadData *currentData, QThreadData *targetData){    Q_Q(QObject);    // 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();    // set new thread data    targetData->ref();    threadData->deref();    threadData = targetData;    for (int i = 0; i < children.size(); ++i) {        QObject *child = children.at(i);        child->d_func()->setThreadData_helper(currentData, targetData);    }}void QObjectPrivate::_q_reregisterTimers(void *pointer){    Q_Q(QObject);    QList<QPair<int, int> > *timerList = reinterpret_cast<QList<QPair<int, int> > *>(pointer);    QAbstractEventDispatcher *eventDispatcher = threadData->eventDispatcher;    for (int i = 0; i < timerList->size(); ++i) {        const QPair<int, int> &pair = timerList->at(i);        eventDispatcher->registerTimer(pair.first, pair.second, q);    }    delete timerList;}//// The timer flag hasTimer is set when startTimer is called.// It is not reset when killing the timer because more than// one timer might be active.///*!    Starts a timer and returns a timer identifier, or returns zero if    it could not start a timer.    A timer event will occur every \a interval milliseconds until    killTimer() is called. If \a interval is 0, then the timer event    occurs once every time there are no more window system events to    process.    The virtual timerEvent() function is called with the QTimerEvent    event parameter class when a timer event occurs. Reimplement this    function to get timer events.    If multiple timers are running, the QTimerEvent::timerId() can be    used to find out which timer was activated.    Example:    \code        class MyObject : public QObject        {            Q_OBJECT        public:            MyObject(QObject *parent = 0);        protected:            void timerEvent(QTimerEvent *event);        };        MyObject::MyObject(QObject *parent)            : QObject(parent)        {            startTimer(50);     // 50-millisecond timer            startTimer(1000);   // 1-second timer            startTimer(60000);  // 1-minute timer        }        void MyObject::timerEvent(QTimerEvent *event)        {            qDebug() << "Timer ID:" << event->timerId();        }    \endcode    Note that QTimer's accuracy depends on the underlying operating    system and hardware. Most platforms support an accuracy of 20    milliseconds; some provide more. If Qt is unable to deliver the    requested number of timer events, it will silently discard some.    The QTimer class provides a high-level programming interface with    single-shot timers and timer signals instead of events. There is    also a QBasicTimer class that is more lightweight than QTimer and    less clumsy than using timer IDs directly.    \sa timerEvent(), killTimer(), QTimer::singleShot()*/int QObject::startTimer(int interval){    Q_D(QObject);    if (interval < 0) {        qWarning("QObject::startTimer: QTimer cannot have a negative interval");        return 0;    }    d->pendTimer = true;                                // set timer flag    if (!d->threadData->eventDispatcher) {        qWarning("QObject::startTimer: QTimer can only be used with threads started with QThread");        return 0;    }    return d->threadData->eventDispatcher->registerTimer(interval, this);}/*!    Kills the timer with timer identifier, \a id.    The timer identifier is returned by startTimer() when a timer    event is started.    \sa timerEvent(), startTimer()*/void QObject::killTimer(int id){    Q_D(QObject);    if (d->threadData->eventDispatcher)        d->threadData->eventDispatcher->unregisterTimer(id);}/*!    \fn QObject *QObject::parent() const    Returns a pointer to the parent object.    \sa children()*//*!    \fn const QObjectList &QObject::children() const    Returns a list of child objects.    The QObjectList class is defined in the \c{<QObject>} header    file as the following:    \quotefromfile src/corelib/kernel/qobject.h    \skipto /typedef .*QObjectList/    \printuntil QObjectList    The first child added is the \l{QList::first()}{first} object in    the list and the last child added is the \l{QList::last()}{last}    object in the list, i.e. new children are appended at the end.    Note that the list order changes when QWidget children are    \l{QWidget::raise()}{raised} or \l{QWidget::lower()}{lowered}. A    widget that is raised becomes the last object in the list, and a    widget that is lowered becomes the first object in the list.    \sa findChild(), findChildren(), parent(), setParent()*/#ifdef QT3_SUPPORTstatic void objSearch(QObjectList &result,                       const QObjectList &list,                       const char  *inheritsClass,                       bool onlyWidgets,                       const char  *objName,                       QRegExp           *rx,                       bool            recurse){    for (int i = 0; i < list.size(); ++i) {        QObject *obj = list.at(i);        bool ok = true;        if (onlyWidgets)            ok = obj->isWidgetType();        else if (inheritsClass && !obj->inherits(inheritsClass))            ok = false;        if (ok) {            if (objName)                ok = (obj->objectName() == QLatin1String(objName));#ifndef QT_NO_REGEXP            else if (rx)                ok = (rx->indexIn(obj->objectName()) != -1);#endif        }        if (ok)                                // match!            result.append(obj);        if (recurse) {            QObjectList clist = obj->children();            if (!clist.isEmpty())                objSearch(result, clist, inheritsClass,                           onlyWidgets, objName, rx, recurse);        }    }}/*!    \internal    Searches the children and optionally grandchildren of this object,

⌨️ 快捷键说明

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