You can use QJSValue for this. Qt Quick Controls 2's SpinBox does something similar:
Q_PROPERTY(QJSValue textFromValue READ textFromValue WRITE setTextFromValue NOTIFY textFromValueChanged FINAL)
Its getters and setters are implemented like this:
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;
}
void QQuickSpinBox::setTextFromValue(const QJSValue &callback)
{
Q_D(QQuickSpinBox);
if (!callback.isCallable()) {
qmlInfo(this) << "textFromValue must be a callable function";
return;
}
d->textFromValue = callback;
emit textFromValueChanged();
}
The getter provides a default function implementation if none was given (or the value isn't actually a function).
The function is used to allow the user to return custom text for a given input value:
text: control.textFromValue(control.value, control.locale)
Taking an example from the documentation, here's how you'd assign/override the function:
SpinBox {
id: spinbox
from: 0
value: 110
to: 100 * 100
stepSize: 100
anchors.centerIn: parent
property int decimals: 2
property real realValue: value / 100
validator: DoubleValidator {
bottom: Math.min(spinbox.from, spinbox.to)
top: Math.max(spinbox.from, spinbox.to)
}
textFromValue: function(value, locale) {
return Number(value / 100).toLocaleString(locale, 'f', spinbox.decimals)
}
valueFromText: function(text, locale) {
return Number.fromLocaleString(locale, text) * 100
}
}