Exposing complex C++ Qt object to QML

放肆的年华 提交于 2019-12-14 04:26:17

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/27300922/exposing-complex-c-qt-object-to-qml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!