c++qtqmlblackberry-cascades

Exposing complex C++ Qt object to QML


In a C++ file, I have an object of the type QList<QStringList>*, which is supposed to be a two dimensional String array.

Currently, in C++ I am able to do this:

// this will display the QString value in the Console,
// where entries is of type QList<QStringList>*
qDebug() << "test: " << entries->at(0).at(0);

I know how to expose this object to QML, but how am I going to be able to navigate / access its functions in QML ?

In main.qml, I can call the function that returns this object:

_app.getCalendar()

But how can I navigate it, like in the C++ code, above?

EDIT: What I actually need to do, is send a two dimensional String array from C++ to QML. Am I doing this in an overly complicated way? Is there another way of accomplishing this?


Solution

  • Define an INVOKABLE getter function in the class you exposed to QML.

    header:

    class MyQmlClass : QObject
    {
        Q_OBJECT
    
    public:
        // ...
    
        Q_INVOKABLE QString getString(int y, int y);
    
    
        // ...
    }
    

    and implement it in the .cpp file ad follows:

    QString MyQmlClass::getString(int x, int y)
    {
        return entries->at(x).at(y);
    }
    

    Finally in QML:

    _app.getCalendar().getString(3, 4)