Javascript 函数作为从 c++ 定义的 QML 属性

Javascript function as QML property defined from c++

本文关键字:定义 QML 属性 c++ 函数 Javascript      更新时间:2023-10-16

我在 c++ 中定义了以下 QML 对象:

class MyObj : public QQuickItem {
    Q_OBJECT
    Q_PROPERTY(QVariant func MEMBER func)
public slots:
    void callFunc(){
        //Call function pointed by "func" somehow
    }
private:
    QVariant func;
};

在QML中,我按如下方式使用MyObj

MyObj{
    func: function test(){ console.log("Hi!"); }
    Button{
        text: "Call func"
        onClicked: parent.callFunc()
    }
}

我收到以下错误:

Unable to assign a function to a property of any type other than var.

我不明白,QVariant属性不应该与property var相同吗?正确的方法是什么?

您可以使用

QJSValue。Qt Quick Controls 2的SpinBox做了类似的事情:

Q_PROPERTY(QJSValue textFromValue READ textFromValue WRITE setTextFromValue NOTIFY textFromValueChanged FINAL)

它的 getter 和 setter 是这样实现的:

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();
}

如果没有给出任何函数(或者该值实际上不是函数(,则 getter 会提供默认函数实现。

该函数用于允许用户返回给定输入值的自定义文本:

text: control.textFromValue(control.value, control.locale)

以文档中的示例为例,以下是分配/覆盖函数的方法:

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
    }
}