[Qt-qml] Q_INVOKABLE member function: Valid argument types

Jonathan Zhong jonathan.zhong at panasonic.aero
Tue Oct 11 23:13:03 CEST 2011


Thanks! This is exactly the Qt documentation page I had been looking 
for. My prior experimentation appears to support what the documentation 
says.

There is one remaining issue: Since int * as an argument does not appear 
working, what if I need to get more than one output integers from the 
Q_INVOKABLE function?

That is, since the following may not work:

// !!! This Q_INVOKABLE function does not work:
void MyClass::myFunc4(int * myOutputInt1, int * myOutputInt2)
{
     *myOutputInt1 = 11111;
     *myOutputInt2 = 22222;
}

one solution is to package the 2 output integers into an object of a Qt 
class that is recognizable by QML, for example, QVariant, as illustrated 
below:

// This Q_INVOKABLE function appears working:
QVariant MyClass::myFunc5()
{
     int myOutputInt1 = 11111;
     int myOutputInt2 = 22222;

     QScriptEngine engine;

     QScriptValue value = engine.newObject();
     value.setProperty("myOutputInt1", myOutputInt1);
     value.setProperty("myOutputInt2", myOutputInt2);

     return value.toVariant();
}

And in my QML code, I'll do something like this:

var myVariant = myClassObject.myFunc5();
var myInt1 = myVariant.myOutputInt1;
var myInt2 = myVariant.myOutputInt2;

This, although it works, does not look simple and straightforward. Is 
there a better way?


On 10/11/2011 11:02 AM, Girish Ramakrishnan wrote:
> Hi Jonathan,
>
> On Tue, Oct 11, 2011 at 10:36 PM, Jonathan Zhong
> <jonathan.zhong at panasonic.aero>  wrote:
>> Hi,
>>
>> What are the valid argument types of an invokable member function of a
>> QML extension C++ class? I have noticed that these work:
>>
>> Q_INVOKABLE void myFunc1(int myInputInt);
>> Q_INVOKABLE void myFunc2(int&  myInputInt);    // but myInputInt cannot
>> be modified.
>>
>> while this does not work:
>>
>> Q_INVOKABLE void myFunc3(int * myOutputInt);    // This will cause a
>> runtime error when running qmlviewer.
>>
>> It appears that:
>> 1. The argument can be an object of (or reference to) a basic C/C++ data
>> type (int, int&, double, double&, ...).
>> 2. The argument can be an object or (or reference to) a Qt class which
>> relates to a basic QML type (QString, QString&, QVariant, QVariant&, ..).
>> 3. The argument can be a pointer to QObject or its derived classes
>> (QObject *, QWidget *, ...).
>> ...
>>
>> Is there a Qt documentation page on this subject?
>>
>> Perhaps this is a related question: What are the valid return types of
>> an invokable member function?
>>
> See http://doc.qt.nokia.com/main-snapshot/qtbinding.html#supported-data-types
>
> Girish



Disclaimer: The information contained in this transmission, including any 
attachments, may contain confidential information of Panasonic Avionics
Corporation.  This transmission is intended only for the use of the 
addressee(s) listed above.  Unauthorized review, dissemination or other use 
of the information contained in this transmission is strictly prohibited. 
If you have received this transmission in error or have reason to believe 
you are not authorized to receive it, please notify the sender by return 
email and promptly delete the transmission.




More information about the Qt-qml mailing list