QML/Javascript内部的QVector

4

如何在QML/Javascript中使用QVector

C++:

我在QML中使用的自定义类。该类包含一个返回已注册ElementTypeQVector的函数。

class CustomType : public QObject
{
    Q_OBJECT

    public:
        Q_INVOKABLE QVector<ElementType*> getElements(); 
    //.....            
}
//.........
qmlRegisterType<CustomType>("CustomComponents", 1, 0, "CustomType");
qmlRegisterType<ElementType>("CustomComponents", 1, 0, "ElementType");
qRegisterMetaType<ElementType*>("ElementType*");

QML:

QML代码接收CustomType类(custom)的实例,并尝试获取元素的QVector<ElementType*>并读取其属性。但是QML无法识别QVector类型。

//.......
function(){
    var elements = custom.getElements()
    elements[0].property   //?
}
3个回答

1

虽然我回答晚了几年,但问题很简单。只需在C++代码中使用Qt QVariantList类型而不是QVector即可解决。Qt的QVariantList本质上是一个包含QVariant元素的QVector。一旦你知道该怎么做,QVariants可以表示几乎任何东西。此外,QML会自动将C++ QVariant转换为其QML等效项。因此,如果您在main.cpp中执行此操作:

#include <QCoreApplication>
#include <QGuiApplication>
#include <QQmlContext>
#include <QQuickView>

int main(int argc, char *argv[])
{
    // Prepare to enter the QML GUI world
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    // Create a QVariantList to be used instead of a QVector
    QVariantList dataList;
    dataList << "Item 1";
    dataList << 2;
    dataList << 3.3;

    // Create QML UI
    QQuickView view;

    // make your QVariantList visible to QML
    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel",
                             QVariant::fromValue(dataList));

    // show QML UI
    view.setSource(QUrl("qrc:/main.qml"));
    view.show();

    return 1;

} // main()

您可以在 main.qml 文件中执行以下操作:

import QtQuick 2.12
import QtQuick.Window 2.12

Item
{
    // Print contents of C++ QVariantList to console
    function printMeBruh()
    {
        console.log("modelData contains:")
        console.log("-------------------")

        for (var i = 0; i < 3; i++)
        {
            console.log(myModel[i])
        }
    }

    Component.onCompleted: printMeBruh()

}

1

QML可访问的列表属性需要使用QDeclarativeListProperty进行构建。这适用于所有形状为列表的内容,包括向量。Qt文档的这一部分详细介绍了此内容。


这不完全是我所需要的。 我添加了: qRegisterMetaType<QVector<ElementType*> >("QVector<ElementType*>"); 在 QML 中,我收到了这种类型:QVariant(QVector<ElementType*>) 那么我该如何访问 QVariant 内部呢? - Svicer

0
在你的示例中,你只需要从一个 Q_INVOKABLE 函数返回一个 QObject*QList。请注意,以这种方式返回的所有对象均具有 JavaScript 拥有权,并且将在 JavaScript 函数返回时进行垃圾回收。为了防止这种行为,在将对象推入 QList 集合之前使用 setObjectOwnership。 有关在 QML 中使用集合的更多信息,请参见QML 数据模型

QList 对于 ListModel 是很好的选择,但在我的情况下,我收到了 QVariant(QList<ElementType*>) 类型的元素,我无法访问 QList<ElementType*> 的内部类型。 - Svicer
你不想尝试从QObjectList继承,并在自己的类中添加一些带有索引参数的访问器方法吗? - Pavel Osipov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接