Solved QList<float> to QML : undefined value
-
Hello,
I am trying to pass a group of values from C++ to QML. In order to archive this, I have append the values that I want in a QList<float> and I made a Q_Property. I used QQmlRegisterType. I follow this process with QStrings and with another QList of doubles.
These are the definitions of the funtions:Q_PROPERTY(QList<float> vfrHud READ vfrHud NOTIFY vfrHudChanged) public: QList<float> vfrHud(); signals: void vfrHudChanged(); slots: void setVfrHud(); private: QList<float> _vfrHud;
And these are the funtions:
void MyTcpSocket::setVfrHud() { _vfrHud=mensajeRec.getVfrHud2(); qDebug()<<"TcpSocket Set"; qDebug()<<_vfrHud; emit vfrHudChanged(); } QList<float> MyTcpSocket::vfrHud() { qDebug()<<"Hola"; qDebug()<<_vfrHud[1]; return _vfrHud; }
As you see, I create a QList from a function of that is defined in another part of the code and the function setVfrHud is called by a signal-slots method. I haven 't any trouble with this part. My problem is that, when I emit the signal vfrHudChanged, the function vfrHud doesn't appears, so when I use the value in my QML code, this value is undefined. Anyone knows how to solve this?
-
@Dooham
see https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#sequence-type-to-javascript-array
onlyQList<double>
is implicitly converted to a JS array. -
@raven-worx Thanks for your help but I have changed the QList<float> for QList<double> and I am still having the same trouble. The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.
-
@Dooham said in QList<float> to QML : undefined value:
The function read of the Q_Property isn't called when the signal vfrHudChanged() is emitted so the value of the property is not updated.
please post the updated C++ code and the QML code where you expect the read method to be called.
The notifier signal only causes the read method to be called when there is a property binding existent (in your QML code). -
@raven-worx This is the updated code (the part of this property):
Q_OBJECT Q_PROPERTY(QList<double> vfrHud READ vfrHud NOTIFY vfrHudChanged) public: QList<double> vfrHud(); signals: void vfrHudChanged(); public slots: void setVfrHud(); private: QList<double> _vfrHud;
QList<double> MyTcpSocket::vfrHud() { qDebug()<<"Hola"; qDebug()<<_vfrHud[1]; return _vfrHud; } void MyTcpSocket::setVfrHud() { _vfrHud=mensajeRec.getVfrHud2(); qDebug()<<"TcpSocket Set"; qDebug()<<_vfrHud; emit vfrHudChanged(); }
I think that the error is in the signal vfrHudChanged(). Because I call the function setVfrHud() the fuction that will write the property thought a signal that is emitted every time I detect the properties has changed (I know that this part work, because I have used the qDebug function to show the value of the variable and that value is correct). But when this signal is emitted the function vfrHud is not called (I know that because the debugger doesnt show me the message "hola").
I have used this method with a previous QList (that one was different) and this method worked but not it doesnt work. -
and the QML code where you expect the read method to be called.
as @raven-worx asked, it would be interesting to check the QML side you're writing...
-
@Pablo-J.-Rogina @raven-worx Sorry, this is the QML part. The code continue with a StackLayout where there is some texts that I want change for the values from the QList, but the part where I introduce the C++ class is here:
import QtQuick 2.9 import QtQuick.Controls 2.5 import QtQuick.Layouts 1.12 import mi.socket 1.0 import "Funciones.js" as Funciones; ApplicationWindow { visible: true width:500 height: 500 minimumHeight: 400 minimumWidth: 400 id: ventana; property variant misAngulos: [1,2,3] property variant vfrQml: [1.0 ,2.0 ,3.0 ,4.0 ] color: "Gray"; title: "GroundStation" MiSocket{ id: mySocket onBatteryChanged: { textBateria.text=battery; } onVibrationsChanged: { textoC.text=vibrations; } onAttitudChanged: { textoY.text=attitud; } onGlobalPositionChanged: { textGlobalPosition.text=globalPosition; } onWindChanged: { textWind.text=wind; } onMissionAckChanged: { textPreassure.text=missionAck; } onAngulosChanged:{ ventana.misAngulos=angulos angulo1.rotation=ventana.misAngulos[0] angulo2.rotation=-ventana.misAngulos[1] angulo3.rotation=-ventana.misAngulos[2] } onVfrHudChanged: { ventana.vfrQml=vfrHud velocidadGround.text=ventana.vfrQml[2] altura.text=ventana.vfrQml[0] climb.text=ventana.vfrQml[3] } }
-
@Dooham said in QList<float> to QML : undefined value:
ventana.vfrQml=vfrHud
can you try
ventana.vfrQml=mySocket.vfrHud
instead. Does it make a difference? -
@raven-worx Thanks you, that works. But I dont understand what is the difference between vfrHud and mySocket.vfrHud
-
You registered an instance of your MyTcpSocket class and the vfrHud is a property of that class.
vfrHud does not exist without your socket class instance.
-
@Dooham
i guess the engine tries to find a variable named vfrHud in the current scope, which dont exist.
Normally you provide a parameter with the notifier signal which is accessed in the slot scope.
But with this way you explicitly read the property named vfrHud