将复杂的C ++ Qt对象暴露给QML [英] Exposing complex C++ Qt object to QML

查看:648
本文介绍了将复杂的C ++ Qt对象暴露给QML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++文件中,我有一个类型为 QList< QStringList> * 的对象,它应该是一个二维String数组。 / p>

目前,在C ++中我可以这样做:

  /这将在控制台中显示QString值,
//其中条目的类型为QList< QStringList> *
qDebug()< test:< entries-> at(0).at(0);

我知道如何将这个对象暴露给QML,但是我怎么能够导航/在QML中访问其函数?



main.qml 中,我可以调用返回此对象的函数: / p>

  _app.getCalendar()

但是我该如何导航它,像在上面的C ++代码?



编辑:我真正需要做的是,发送一个两三维字符串数组从C ++到QML。我这是一个过于复杂的方式吗?是否有其他方法可以完成此操作?

解决方案

定义 INVOKABLE getter

  

class MyQmlClass:QObject
{
Q_OBJECT

public:
// ...

Q_INVOKABLE QString getString(int y,int y );


// ...
}

并在.cpp文件中实现它:

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

最后在QML中:

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


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?

解决方案

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)

这篇关于将复杂的C ++ Qt对象暴露给QML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆