user7349647
user7349647

Reputation: 31

How to call a non-parameterised QML function from C++

I am trying to call a function in my QML:

function resetSomething() {
    tempVar= undefined
}

I am not sure how to write the invokeMethod for this function. I tried using something like that:

QQuickItem* qObj= findChild<QQuickItem>("temp");
if (qObj)
{
    QVariant returnArg;
    QMetaObject::invokeMethod(qObj, "resetSomething",Q_RETURN_ARG,QVariant,returnArg));
}

But it gives the error

no matching function resetSomething(QVariant) found.

Upvotes: 2

Views: 314

Answers (1)

QtRoS
QtRoS

Reputation: 1177

As you can see in your error message Qt's Meta Object System is trying to find method resetSomething(QVariant), but your method doesn't have any parameters. I guess it's because of wrong Q_RETURN_ARG macro usage.
Seems that you can simply fix it like this:

QVariant returnArg;
QMetaObject::invokeMethod(qObj, "resetSomething", Qt::DirectConnection,
                          Q_RETURN_ARG(QVariant, returnArg));

Also I suggest you to read official Qt documentation, it is brilliant.

BTW: Nice repo from Google with a lot of C++/JavaScript intercommunication. You can find a lot of useful patterns there.

Upvotes: 1

Related Questions