I have a two dimensional float array in QML. How do I get its values in C++.
I have created a class in c++ and have done the part of qmlRegisterType. The class is now accessible in QML.
Please demonstrate with a small example.
This is what I have tried:
#ifndef TEST_H
#define TEST_H
#include <QObject>
#include <QQuickItem>
#include <iostream>
{
private:
Q_OBJECT
Q_PROPERTY(QVariantList name READ name WRITE setName)
QVariantList m_name;
public:
QVariantList name() const
{
return m_name;
}
public slots:
void setName (QVariantList arg)
{
m_name = arg;
QVector<QVariant> p = m_name.toVector ();
std::cout << p.first ().toInt ();
}
};
#endif // TEST_H
#ifndef TEST_H
#define TEST_H
#include <QObject>
#include <QQuickItem>
#include <iostream>
class Test: public QObject
{
private:
Q_OBJECT
Q_PROPERTY(QVariantList name READ name WRITE setName)
QVariantList m_name;
public:
QVariantList name() const
{
return m_name;
}
public slots:
void setName (QVariantList arg)
{
m_name = arg;
QVector<QVariant> p = m_name.toVector ();
std::cout << p.first ().toInt ();
}
};
#endif // TEST_H
To copy to clipboard, switch view to plain text mode
The c++ part is giving me a segmentation fault.
QML:
import QtQuick 2.2
import QtQuick.Window 2.1
import AGVUI 1.0
Window {
visible: true
width: 360
height: 360
function createTwoDimensionalArray (twoDimArray, rows, cols)
{
/// Creates all lines.
for (var i = 0; i < rows; i++)
{
/// Creates an empty line.
twoDimArray.push ([]);
/// Ads colunms to the empty line.
twoDimArray [i].push (new Array (cols));
for (var j = 0; j < cols; j++)
{
/// Default initialization to zero
twoDimArray [i][j] = 0
}
}
return twoDimArray
}
Test
{
Component.onCompleted:
{
var arr = []
createTwoDimensionalArray (arr, 2, 2)
setName(arr)
}
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
import QtQuick 2.2
import QtQuick.Window 2.1
import AGVUI 1.0
Window {
visible: true
width: 360
height: 360
function createTwoDimensionalArray (twoDimArray, rows, cols)
{
/// Creates all lines.
for (var i = 0; i < rows; i++)
{
/// Creates an empty line.
twoDimArray.push ([]);
/// Ads colunms to the empty line.
twoDimArray [i].push (new Array (cols));
for (var j = 0; j < cols; j++)
{
/// Default initialization to zero
twoDimArray [i][j] = 0
}
}
return twoDimArray
}
Test
{
Component.onCompleted:
{
var arr = []
createTwoDimensionalArray (arr, 2, 2)
setName(arr)
}
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks