How to pass a 2 dimensional float array from QML to C++?
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:
Code:
#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
The c++ part is giving me a segmentation fault.
QML:
Code:
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
}
}
Re: How to pass a 2 dimensional float array from QML to C++?
What did you try to debug it so far?
What did the stack trace of the crash tell you?
Have checked the size of the "arg" list? Does the list contain at least one element?
Cheers,
_
Re: How to pass a 2 dimensional float array from QML to C++?
Here's what I have tried:
Header:
Code:
#include <QQuickItem>
#include <iostream>
{
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
);
};
qml
Code:
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)
}
}
}
The exact output from QtCreator:
Code:
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
/home/***/documents/test/build-junk-Desktop_Qt_5_1_0_GCC_64bit-Debug/junk exited with code 0
///
Here, as you can see the size of the 2D array from QML is printed correctly as 2 and the size is printed 0 from c++.
Why is that happening? Please explain.
Re: How to pass a 2 dimensional float array from QML to C++?
Where is the segmentation fault? What is the backtrace for it? Why did you choose QList<QVariantList> as the argument type? Why not use QVariant instead? What is the effect you wish to obtain? Maybe a QQmlListProperty would be a better solution?
Re: How to pass a 2 dimensional float array from QML to C++?
It is strange that the console.log output would happen before the C++ log output despite it being after the call to the slot.
Have you tried a one dimensional array?
What do you get as C++ output when you have your orignal argument type, i.e. QVariantList?
Cheers,
_
Re: How to pass a 2 dimensional float array from QML to C++?
Thanks to both of you for your time.
In the second attempt of mine, I didn't get a segmenation fault.
When I tried to print the size of the argument, it printed 0. That was the problem.
From another QA site I came to know that `QVariantList` as an argument is sufficient.
What I didn't know was that to access each row we first need to convert it to a list.
The following works now:
Code:
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 ();
}
}
Quote:
Originally Posted by
anda_skoa
It is strange that the console.log output would happen before the C++ log output despite it being after the call to the slot.
This is something that is still occurring and I don't know what causes it.