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

Arrays 如何将二维浮点数组从QML传递到C++;?,arrays,qt,qml,qtquick2,Arrays,Qt,Qml,Qtquick2,我在QML中有一个二维浮点数组。如何在C++中获得其值?< /P> 我在C++中创建了一个类,完成了QMLReavestType < /C> >的部分。该类现在可以在QML中访问 请用一个小例子来演示 以下是我尝试过的: 标题: #include <QQuickItem> #include <iostream> class Controller : public QObject { Q_OBJECT Q_PROPERTY(QList <QVar

我在QML中有一个二维浮点数组。如何在C++中获得其值?< /P>

我在C++中创建了一个类,完成了QMLReavestType < /C> >的部分。该类现在可以在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);
};
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
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)
        }
    }
}
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 ();
    }
}
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
QtCreator的确切输出:

#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);
};
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
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)
        }
    }
}
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 ();
    }
}
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
///

<>强>这里,从QML中可以看到二维数组的大小被正确打印为2,大小从C++打印0。< /强> < /P> 为什么会这样?请解释。

试试这个:

void setnames(QVariantList const& arg)
    {
        if (arg.size())
        {
            auto const p(arg.front().toList());
            std::cout << "\narg: \n" << p.front().toInt();
        }
        else
            std::cout << "\nqqqq " << arg.size () << "\n";
    }
QML文件中的某处:

property variant floats: [1.1, 2.2, 3.3, 4.4, 5.5, 6.6]
包括:

#include <QtQuick/QQuickView>
#include <QQmlContext>
#include <QQmlProperty>
#include <QQuickItem>

我希望我的代码显示了主要思想,您可以在课堂上使用它,因为最重要的工作(如何获取数字)已经完成。

看起来您可以使用
QVariantList
(或
QList
):


其实很简单。Javascript数组映射到
QVariantList
s,Javascript对象映射到
QVariantMap
s。因此,您正在处理嵌套的
QVariantList
s。试试这个:

void setnames(QVariantList const& arg)
    {
        if (arg.size())
        {
            auto const p(arg.front().toList());
            std::cout << "\narg: \n" << p.front().toInt();
        }
        else
            std::cout << "\nqqqq " << arg.size () << "\n";
    }
void集合名(QVariantList const&arg)
{
if(arg.size())
{
自动常数p(参数front().toList());
STD::CUT

,根据,为了访问C++传递给QC++的2个模糊数组的内部元素,我们需要将每行转换成一个列表: 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);
};
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
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)
        }
    }
}
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 ();
    }
}
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
C++部分:

#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);
};
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
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)
        }
    }
}
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 ();
    }
}
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
这是我的方法:

#include <QQuickItem>
#include <QDebug>

#include <QVariantList>

class Controller : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QVariant names READ names WRITE setnames NOTIFY namesChanged)
    QVariant m_names;

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

    QVariant names() const
    {
        return m_names;
    }

public slots:
    void setnames(QVariant arg)
    {
        QVariantList dim1 = arg.toList();
        qDebug() << "outer dimension size" <<  dim1.size();
        for(int i=0;i<dim1.size();++i)
        {
            QVariantList &dim2 = dim1.at(i).toList();
            qDebug() << "inner dimension size at" << dim2.size();
        }
    }

signals:
    void namesChanged();
};
#包括
#包括
#包括
类控制器:公共QObject
{
Q_对象
Q_属性(QVariant名称读取名称写入集合名称通知名称更改)
qm_名称;
公众:
控制器()
{
}
~Controller(){
}
QVariant names()常量
{
返回m_名称;
}
公众时段:
无效集合名(QVariant arg)
{
QVariantList dim1=arg.toList();

qDebug()虽然你的程序给了我一些语法错误,但我还是认为我们需要首先将每一行转换为
toList
,并将其存储在一个列表中,然后才能访问该行的每一点。谢谢。