如何将二维浮点数组从 QML 传递到 C++?

Aqu*_*irl 2 arrays qt qml qtquick2

我在 QML 中有一个二维浮点数组。我如何在 C++ 中获取它的值。

我用 C++ 创建了一个类,并完成了qmlRegisterType. 现在可以在 QML 中访问该类。

请用一个小例子来演示。


这是我尝试过的:

标题:

#include <QQuickItem>
#include <iostream>

class Controller : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QList <QVariantList> names READ names WRITE setnames NOTIFY namesChanged)
    QList <QVariantList> m_names;

public:
    Controller()
    {
    }
    ~Controller() {
    }

    QList <QVariantList> names() const
    {
        return m_names;
    }

public slots:
    void setnames(QList <QVariantList> arg)
    {
        QVariantList p;
        if (arg.size () > 0)
        {
            p = arg.first ();
            std::cout << "\narg: \n" << p[0].toInt ();
        }
        else
            std::cout << "\nqqqq " << arg.size () << "\n";
    }

signals:
    void namesChanged(QList <QVariantList> arg);
};
Run Code Online (Sandbox Code Playgroud)

qml

import QtQuick 2.0
import FromCpp 1.0

Rectangle
{
    property variant arras: [[1,2,3], [4,5,6]]
    Controller
    {
        id: ppp
    }

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            ppp.setnames(arras)
            console.log(arras.length)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

QtCreator 的确切输出:

Starting /home/***/documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
QML debugging is enabled. Only use this in a safe environment.
2

qqqq 0
QThreadStorage: Thread 0x181e270 exited after QThreadStorage 2 destroyed
/home/***/documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0
Run Code Online (Sandbox Code Playgroud)

///

在这里,正如您所看到的,QML 中的 2D 数组的大小正确打印为 2,而 c++ 中的大小打印为 0。

为什么会发生这种情况?请解释。

Aqu*_*irl 5

根据user1095108的回答,为了访问传递给 C++ 的 QML 2 Dim 数组的内部元素,我们需要将每一行转换为列表,如下所示:

QML部分:

import QtQuick 2.0
import FromCpp 1.0

Rectangle
{
    property variant twoDimArray: [[1,2,3], [4,5,6]]
    Controller
    {
        id: controllerA
    }

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            controllerA.setname (twoDimArray)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

C++部分:

void setname (QVariantList arg)  
{  
    if (arg.size())
    {
        QList <QVariant> p = arg[0].toList();

        std::cout << "\nRow0 0:" << p[0].toInt ();
        std::cout << "\nRow0 1:" << p[1].toInt ();
        std::cout << "\nRow0 2:" << p[2].toInt ();

        std::cout << "\n";

        QList <QVariant> p1 = arg[1].toList();

        std::cout << "\nRow1 0:" << p1[0].toInt ();
        std::cout << "\nRow1 1:" << p1[1].toInt ();
        std::cout << "\nRow1 2:" << p1[2].toInt ();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Starting /home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk...
QML debugging is enabled. Only use this in a safe environment.

Row0 0:1
Row0 1:2
Row0 2:3

Row1 0:4
Row1 1:5
Row1 2:6/home/.../documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0
Run Code Online (Sandbox Code Playgroud)