qapplication.cpp

来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 2,012 行 · 第 1/5 页

CPP
2,012
字号
/*!    Changes the default application font to \a font. If \a className    is passed, the change applies only to classes that inherit \a    className (as reported by QObject::inherits()).    On application start-up, the default font depends on the window    system. It can vary depending on both the window system version and    the locale. This function lets you override the default font; but    overriding may be a bad idea because, for example, some locales need    extra large fonts to support their special characters.    \sa font(), fontMetrics(), QWidget::setFont()*/void QApplication::setFont(const QFont &font, const char *className){    bool all = false;    FontHash *hash = app_fonts();    if (!className) {        qt_app_has_font = true;        if (!QApplicationPrivate::app_font)            QApplicationPrivate::app_font = new QFont(font);        else            *QApplicationPrivate::app_font = font;        if (hash && hash->size()) {            all = true;            hash->clear();        }    } else if (hash) {        hash->insert(className, font);    }    if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {        QEvent e(QEvent::ApplicationFontChange);        QWidgetList wids = QApplication::allWidgets();        for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) {            register QWidget *w = *it;            if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class                sendEvent(w, &e);        }    }}/*!    \property QApplication::windowIcon    \brief the default window icon    \sa QWidget::setWindowIcon(), {Setting the Application Icon}*/QIcon QApplication::windowIcon(){    return QApplicationPrivate::app_icon ? *QApplicationPrivate::app_icon : QIcon();}void QApplication::setWindowIcon(const QIcon &icon){    if (!QApplicationPrivate::app_icon)        QApplicationPrivate::app_icon = new QIcon();    *QApplicationPrivate::app_icon = icon;    if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {#ifdef Q_WS_MAC        void qt_mac_set_app_icon(const QPixmap &); //qapplication_mac.cpp        QSize size = QApplicationPrivate::app_icon->actualSize(QSize(128, 128));        qt_mac_set_app_icon(QApplicationPrivate::app_icon->pixmap(size));#endif        QEvent e(QEvent::ApplicationWindowIconChange);        QWidgetList all = QApplication::allWidgets();        for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) {            register QWidget *w = *it;            if (w->isWindow())                sendEvent(w, &e);        }    }}/*!    Returns a list of the top-level widgets (windows) in the    application.    Note that some of the top-level widgets may be hidden, for    example a tooltip if no tooltip is currently shown.    Example:    \code        void showAllHiddenTopLevelWidgets()        {            foreach (QWidget *widget, QApplication::topLevelWidgets()) {                if (widget->isHidden())                    widget->show();            }        }    \endcode    \sa allWidgets(), QWidget::isWindow(), QWidget::isHidden()*/QWidgetList QApplication::topLevelWidgets(){    QWidgetList list;    QWidgetList all = allWidgets();    for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) {        QWidget *w = *it;        if (w->isWindow() && w->windowType() != Qt::Desktop)            list.append(w);    }    return list;}/*!    Returns a list of all the widgets in the application.    The list is empty (QList::isEmpty()) if there are no widgets.    Note that some of the widgets may be hidden.    Example:    \code        void updateAllWidgets()        {            foreach (QWidget *widget, QApplication::allWidgets())                widget->update();        }    \endcode    \sa topLevelWidgets(), QWidget::isVisible()*/QWidgetList QApplication::allWidgets(){    QWidgetList list;    if (QWidgetPrivate::mapper)        list += QWidgetPrivate::mapper->values();    if (QWidgetPrivate::uncreatedWidgets)        list += QWidgetPrivate::uncreatedWidgets->toList();    return list;}/*!  Returns the application widget that has the keyboard input focus, or  0 if no widget in this application has the focus.  \sa QWidget::setFocus(), QWidget::hasFocus(), activeWindow(), focusChanged()*/QWidget *QApplication::focusWidget(){    return QApplicationPrivate::focus_widget;}void QApplicationPrivate::setFocusWidget(QWidget *focus, Qt::FocusReason reason){    if (focus != focus_widget) {        if (focus && (reason == Qt::BacktabFocusReason || reason == Qt::TabFocusReason)            && qt_in_tab_key_event)            focus->window()->setAttribute(Qt::WA_KeyboardFocusChange);        else if (focus && reason == Qt::ShortcutFocusReason) {            focus->window()->setAttribute(Qt::WA_KeyboardFocusChange);        }        QWidget *prev = focus_widget;        if (prev && reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason) {            QInputContext *qic = prev->inputContext();            if(qic) {                qic->reset();                qic->setFocusWidget(0);            }        }        focus_widget = focus;        if (reason != Qt::NoFocusReason) {            //send events            if (prev) {#ifdef QT_KEYPAD_NAVIGATION                if (QApplication::keypadNavigationEnabled()) {                    if (prev->hasEditFocus())                        prev->setEditFocus(false);                }#endif                QFocusEvent out(QEvent::FocusOut, reason);                QStyle *style = prev->style();                QApplication::sendEvent(prev, &out);                QApplication::sendEvent(style, &out);            }            if(focus && QApplicationPrivate::focus_widget == focus) {                QInputContext *qic = focus->inputContext();                if (qic && focus_widget->testAttribute(Qt::WA_WState_Created))	            qic->setFocusWidget( focus_widget );                QFocusEvent in(QEvent::FocusIn, reason);                QApplication::sendEvent(focus, &in);                QApplication::sendEvent(focus->style(), &in);            }        }        emit qApp->focusChanged(prev, focus_widget);    }}/*!  Returns the application top-level window that has the keyboard input  focus, or 0 if no application window has the focus. Note that  there might be an activeWindow() even if there is no focusWidget(),  for example if no widget in that window accepts key events.  \sa QWidget::setFocus(), QWidget::hasFocus(), focusWidget()*/QWidget *QApplication::activeWindow(){    return QApplicationPrivate::active_window;}/*!  Returns display (screen) font metrics for the application font.  \sa font(), setFont(), QWidget::fontMetrics(), QPainter::fontMetrics()*/QFontMetrics QApplication::fontMetrics(){    return desktop()->fontMetrics();}/*!    Closes all top-level windows.    This function is particularly useful for applications with many    top-level windows. It could, for example, be connected to a    \gui{Exit} entry in the \gui{File} menu:    \quotefromfile mainwindows/mdi/mainwindow.cpp    \skipto exitAct = new QAct    \printuntil SLOT(closeAllWindows())    The windows are closed in random order, until one window does not    accept the close event. The application quits when the last window    was successfully closed; this can be turned off by setting \l    quitOnLastWindowClosed to false.    \sa quitOnLastWindowClosed, lastWindowClosed()  QWidget::close(), QWidget::closeEvent(), lastWindowClosed(),        quit(), topLevelWidgets(), QWidget::isWindow()*/void QApplication::closeAllWindows(){    bool did_close = true;    QWidget *w;    while((w = activeModalWidget()) && did_close) {        if(!w->isVisible())            break;        did_close = w->close();    }    QWidgetList list = QApplication::topLevelWidgets();    for (int i = 0; did_close && i < list.size(); ++i) {        w = list.at(i);        if (w->isVisible() && w->windowType() != Qt::Desktop) {            did_close = w->close();            list = QApplication::topLevelWidgets();            i = -1;        }    }}/*!    Displays a simple message box about Qt. The message includes the    version number of Qt being used by the application.    This is useful for inclusion in the Help menu of an application.    See the examples/menu/menu.cpp example.    This function is a convenience slot for QMessageBox::aboutQt().*/void QApplication::aboutQt(){#ifndef QT_NO_MESSAGEBOX    QMessageBox::aboutQt(activeWindow());#endif // QT_NO_MESSAGEBOX}/*!    \fn void QApplication::lastWindowClosed()    This signal is emitted from QApplication::exec() when the last    visible primary window (i.e. window with no parent) with the    Qt::WA_QuitOnClose attribute set is closed.    By default,    \list    \i this attribute is set for all widgets except transient windows    such as splash screens, tool windows, and popup menus    \i QApplication implicitly quits when this signal is emitted.    \endlist    This feature be turned off by setting \l quitOnLastWindowClosed to    false.    \sa QWidget::close()*//*!    \since 4.1    \fn void QApplication::focusChanged(QWidget *old, QWidget *now)    This signal is emitted when the widget that has keyboard focus    changed from \a old to \a now, i.e. because the user pressed the    tab-key, clicked into a widget or changed the active window. Note    that both \a old and \a now can be the null-pointer.    The signal is emitted after both widget have been notified about    the change through QFocusEvent.    \sa QWidget::setFocus() QWidget::clearFocus() Qt::FocusReason*/#ifndef QT_NO_TRANSLATIONstatic bool qt_detectRTLLanguage(){    return force_reverse ^        QApplication::tr("QT_LAYOUT_DIRECTION",                         "Translate this string to the string 'LTR' in left-to-right"                         " languages or to 'RTL' in right-to-left languages (such as Hebrew"                         " and Arabic) to get proper widget layout.") == QLatin1String("RTL");}#endif/*!\reimp*/bool QApplication::event(QEvent *e){    Q_D(QApplication);    if(e->type() == QEvent::Close) {        QCloseEvent *ce = static_cast<QCloseEvent*>(e);        ce->accept();        closeAllWindows();        QWidgetList list = topLevelWidgets();        for (int i = 0; i < list.size(); ++i) {            QWidget *w = list.at(i);            if (w->isVisible() && !(w->windowType() == Qt::Desktop) && !(w->windowType() == Qt::Popup) &&                 (!(w->windowType() == Qt::Dialog) || !w->parentWidget())) {                ce->ignore();                break;            }        }        if(ce->isAccepted())            return true;    } else if(e->type() == QEvent::LanguageChange) {#ifndef QT_NO_TRANSLATION        setLayoutDirection(qt_detectRTLLanguage()?Qt::RightToLeft:Qt::LeftToRight);#endif        QWidgetList list = topLevelWidgets();        for (int i = 0; i < list.size(); ++i) {            QWidget *w = list.at(i);            if (!(w->windowType() == Qt::Desktop))                postEvent(w, new QEvent(QEvent::LanguageChange));        }    } else if (e->type() == QEvent::Timer) {        QTimerEvent *te = static_cast<QTimerEvent*>(e);        Q_ASSERT(te != 0);        if (te->timerId() == d->toolTipWakeUp.timerId()) {            d->toolTipWakeUp.stop();            d->toolTipFallAsleep.start(2000, this);            if (d->toolTipWidget) {                QHelpEvent e(QEvent::ToolTip, d->toolTipPos, d->toolTipGlobalPos);                QApplication::sendEvent(d->toolTipWidget, &e);            }        } else if (te->timerId() == d->toolTipFallAsleep.timerId()) {            d->toolTipFallAsleep.stop();        }    }    return QCoreApplication::event(e);}#if !defined(Q_WS_X11)// The doc and X implementation of this function is in qapplication_x11.cppvoid QApplication::syncX()        {}                // do nothing#endif/*!    \fn Qt::WindowsVersion QApplication::winVersion()    Use \l QSysInfo::WindowsVersion instead.*//*!    \fn void QApplication::setActiveWindow(QWidget* active)    Sets the active window to the \a active widget in response to a system    event. The function is called from the platf

⌨️ 快捷键说明

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