From 1f146d982b36e0113558806f580da0d32cefef49 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 29 Jan 2016 09:46:25 +0100 Subject: QQuickSpinBox: create JS callbacks lazily Calling QJSEngine::evaluate() is a heavy operation. Only one of them, namely textFromValue, is used from QML, so there's a good chance that valueFromText doesn't need to be evaluated at component creation time. This change postpones the initial evaluation to the point where either callback is requested. Before: RESULT : tst_CreationTime::controls():"SpinBox": 0.53 msecs per iteration (total: 68, iterations: 128) After: RESULT : tst_CreationTime::controls():"SpinBox": 0.39 msecs per iteration (total: 51, iterations: 128) Change-Id: I1730dc4024d0a556ca2da765ac3c52ba8a29f743 Reviewed-by: Mitch Curtis --- src/templates/qquickspinbox.cpp | 27 ++++++++++++--------------- src/templates/qquickspinbox_p.h | 1 - 2 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/templates/qquickspinbox.cpp b/src/templates/qquickspinbox.cpp index 33ea6e79..bda955d0 100644 --- a/src/templates/qquickspinbox.cpp +++ b/src/templates/qquickspinbox.cpp @@ -116,8 +116,8 @@ public: QQuickSpinButton *up; QQuickSpinButton *down; QValidator *validator; - QJSValue textFromValue; - QJSValue valueFromText; + mutable QJSValue textFromValue; + mutable QJSValue valueFromText; }; int QQuickSpinBoxPrivate::boundValue(int value) const @@ -384,6 +384,11 @@ void QQuickSpinBox::setValidator(QValidator *validator) QJSValue QQuickSpinBox::textFromValue() const { Q_D(const QQuickSpinBox); + if (!d->textFromValue.isCallable()) { + QQmlEngine *engine = qmlEngine(this); + if (engine) + d->textFromValue = engine->evaluate(QStringLiteral("function(value, locale) { return Number(value).toLocaleString(locale, 'f', 0); }")); + } return d->textFromValue; } @@ -420,6 +425,11 @@ void QQuickSpinBox::setTextFromValue(const QJSValue &callback) QJSValue QQuickSpinBox::valueFromText() const { Q_D(const QQuickSpinBox); + if (!d->valueFromText.isCallable()) { + QQmlEngine *engine = qmlEngine(this); + if (engine) + d->valueFromText = engine->evaluate(QStringLiteral("function(text, locale) { return Number.fromLocaleString(locale, text); }")); + } return d->valueFromText; } @@ -587,19 +597,6 @@ void QQuickSpinBox::timerEvent(QTimerEvent *event) } } -void QQuickSpinBox::componentComplete() -{ - Q_D(QQuickSpinBox); - QQuickControl::componentComplete(); - QQmlEngine *engine = qmlEngine(this); - if (engine) { - if (!d->textFromValue.isCallable()) - setTextFromValue(engine->evaluate(QStringLiteral("function(value, locale) { return Number(value).toLocaleString(locale, 'f', 0); }"))); - if (!d->valueFromText.isCallable()) - setValueFromText(engine->evaluate(QStringLiteral("function(text, locale) { return Number.fromLocaleString(locale, text); }"))); - } -} - void QQuickSpinBox::itemChange(ItemChange change, const ItemChangeData &value) { Q_D(QQuickSpinBox); diff --git a/src/templates/qquickspinbox_p.h b/src/templates/qquickspinbox_p.h index 0ad5e70d..c7d1f6c6 100644 --- a/src/templates/qquickspinbox_p.h +++ b/src/templates/qquickspinbox_p.h @@ -121,7 +121,6 @@ protected: void mouseUngrabEvent() Q_DECL_OVERRIDE; void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE; - void componentComplete() Q_DECL_OVERRIDE; void itemChange(ItemChange change, const ItemChangeData &value) Q_DECL_OVERRIDE; void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) Q_DECL_OVERRIDE; -- cgit v1.2.3