1

Here is the documentation for the invokeMethod function.

So if I pass a string or QString type as argument, then it works. But is it possible to pass a list, vector or array of QString as argument?

P.S: The argument has to be a type of QGenericArgument and I have to use the Q_ARG macro to create the QGenericArgument object.

2 Answers 2

3

Yes. You must register the type so that Qt can handle it correctly.

On how to register a type see qRegisterMetaType and Q_DECLARE_METATYPE

Qt types are already registered. So passing e.g. a QStringlist via Q_ARG shouldn't be a problem at all.

QString retVal;
QStringList values = QStringList() << "aaa" << "bb" << "cccc";
QMetaObject::invokeMethod(obj, "findLongest", Qt::DirectConnection,
                      Q_RETURN_ARG(QString, retVal),
                      Q_ARG(QStringList , values));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks invokeMethod function worked! And how do I process the QStringList values in the findLongest function? How can I let the function know, that it is a type of QStringList (in QML)?
@OnurA I haven't worked with QML yet. Usually the invoked method is invokable or a slot. In this example it would most probably look somehow like this: QString findLongest(QStringList list);
2

I found the following documentation. You can pass a QVariantList and then read the list in your QML file.

C++

QVariantList list;
list << 10 << QColor(Qt::green) << "bottles";


QMetaObject::invokeMethod(view.rootObject(), "readValues",
        Q_ARG(QVariant, QVariant::fromValue(list)));

QML:

 function readValues(anArray) {
    for (var i=0; i<anArray.length; i++)
        console.log("Array item:", anArray[i])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.