aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2016-01-29 09:46:25 +0100
committerSimon Hausmann <simon.hausmann@theqtcompany.com>2016-01-29 15:10:36 +0000
commit1f146d982b36e0113558806f580da0d32cefef49 (patch)
treeb98a1808ca68d27f01ad69372de7a1a2a4a6de0a /src
parent584b768cd051deb1da28cad5b9aaa15975c05b8d (diff)
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 <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/templates/qquickspinbox.cpp27
-rw-r--r--src/templates/qquickspinbox_p.h1
2 files changed, 12 insertions, 16 deletions
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;