From 3f84c18ac0f4109906be4a80c3a1480b032e2e17 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 19 Feb 2015 13:51:46 +0100 Subject: Micro-optimize QApplication. Avoid repeated instantiation of end() in loops, use variable instead. Change-Id: I5f58fa63c2845827ebe9be2d0fcee80b7ccc74bc Reviewed-by: Marc Mutz --- src/widgets/kernel/qapplication.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index d19087bd8d..d097313b9b 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -827,7 +827,7 @@ QApplication::~QApplication() if (QWidgetPrivate::allWidgets) { QWidgetSet *mySet = QWidgetPrivate::allWidgets; QWidgetPrivate::allWidgets = 0; - for (QWidgetSet::ConstIterator it = mySet->constBegin(); it != mySet->constEnd(); ++it) { + for (QWidgetSet::ConstIterator it = mySet->constBegin(), cend = mySet->constEnd(); it != cend; ++it) { QWidget *w = *it; if (!w->parent()) // window w->destroy(true, true); @@ -1190,7 +1190,7 @@ void QApplication::setStyle(QStyle *style) // clean up the old style if (QApplicationPrivate::app_style) { if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { - for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) { + for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) { QWidget *w = *it; if (!(w->windowType() == Qt::Desktop) && // except desktop w->testAttribute(Qt::WA_WState_Polished)) { // has been polished @@ -1238,8 +1238,8 @@ void QApplication::setStyle(QStyle *style) // re-polish existing widgets if necessary if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { - for (QWidgetList::ConstIterator it1 = all.constBegin(); it1 != all.constEnd(); ++it1) { - QWidget *w = *it1; + for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) { + QWidget *w = *it; if (w->windowType() != Qt::Desktop && w->testAttribute(Qt::WA_WState_Polished)) { if (w->style() == QApplicationPrivate::app_style) QApplicationPrivate::app_style->polish(w); // repolish @@ -1250,8 +1250,8 @@ void QApplication::setStyle(QStyle *style) } } - for (QWidgetList::ConstIterator it2 = all.constBegin(); it2 != all.constEnd(); ++it2) { - QWidget *w = *it2; + for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) { + QWidget *w = *it; if (w->windowType() != Qt::Desktop && !w->testAttribute(Qt::WA_SetStyle)) { QEvent e(QEvent::StyleChange); QApplication::sendEvent(w, &e); @@ -1416,12 +1416,15 @@ void QApplication::setGlobalStrut(const QSize& strut) */ QPalette QApplication::palette(const QWidget* w) { + typedef PaletteHash::const_iterator PaletteHashConstIt; + PaletteHash *hash = app_palettes(); if (w && hash && hash->size()) { - QHash::ConstIterator it = hash->constFind(w->metaObject()->className()); - if (it != hash->constEnd()) + PaletteHashConstIt it = hash->constFind(w->metaObject()->className()); + const PaletteHashConstIt cend = hash->constEnd(); + if (it != cend) return *it; - for (it = hash->constBegin(); it != hash->constEnd(); ++it) { + for (it = hash->constBegin(); it != cend; ++it) { if (w->inherits(it.key())) return it.value(); } @@ -1480,7 +1483,7 @@ void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char* QApplication::sendEvent(QApplication::instance(), &e); QWidgetList wids = QApplication::allWidgets(); - for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) { + for (QWidgetList::ConstIterator it = wids.constBegin(), cend = wids.constEnd(); it != cend; ++it) { QWidget *w = *it; if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class QApplication::sendEvent(w, &e); @@ -1582,6 +1585,8 @@ QFont QApplication::font() QFont QApplication::font(const QWidget *widget) { + typedef FontHash::const_iterator FontHashConstIt; + FontHash *hash = app_fonts(); if (widget && hash && hash->size()) { @@ -1593,11 +1598,11 @@ QFont QApplication::font(const QWidget *widget) return hash->value(QByteArrayLiteral("QMiniFont")); } #endif - QHash::ConstIterator it = - hash->constFind(widget->metaObject()->className()); - if (it != hash->constEnd()) + FontHashConstIt it = hash->constFind(widget->metaObject()->className()); + const FontHashConstIt cend = hash->constEnd(); + if (it != cend) return it.value(); - for (it = hash->constBegin(); it != hash->constEnd(); ++it) { + for (it = hash->constBegin(); it != cend; ++it) { if (widget->inherits(it.key())) return it.value(); } @@ -1662,7 +1667,7 @@ void QApplication::setFont(const QFont &font, const char *className) QApplication::sendEvent(QApplication::instance(), &e); QWidgetList wids = QApplication::allWidgets(); - for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) { + for (QWidgetList::ConstIterator it = wids.constBegin(), cend = wids.constEnd(); it != cend; ++it) { QWidget *w = *it; if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class sendEvent(w, &e); @@ -1767,7 +1772,7 @@ QWidgetList QApplication::topLevelWidgets() QWidgetList list; QWidgetList all = allWidgets(); - for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) { + for (QWidgetList::ConstIterator it = all.constBegin(), cend = all.constEnd(); it != cend; ++it) { QWidget *w = *it; if (w->isWindow() && w->windowType() != Qt::Desktop) list.append(w); -- cgit v1.2.3