Lauriane.C Lauriane.C - 3 months ago 16
C++ Question

QML interaction with CPP : property double results in NaN

Here is my QML file that is a simple Rectangle where I want to display double values excerpted from my cpp code (yaw, roll and pitch).

tilt.qml :

import QtQuick 2.5
Rectangle {
id: myTilt
width: 100
height: 80
color: "grey"
border.color: "black"
anchors.margins: 15
property double yaw: 1.0 ;
property double roll: 0 ;
property double pitch: 0;


MouseArea {
anchors.fill: parent
onClicked: {
parent.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1);
}
}

Text {
id: tilt
text: qsTr("Tilt :")
anchors.margins: 3
anchors.top: page.top
anchors.left: page.left
}

Text {
id: yaw
text: qsTr("Yaw : ") + Math.abs(yaw)
anchors.margins: 5
anchors.top: tilt.bottom
anchors.left: tilt.left
}
Text {
id: roll
text: qsTr("Roll : ") + roll
anchors.margins: 0
anchors.top: yaw.bottom
anchors.left: yaw.left
}
Text {
id: pitch
text: qsTr("Pitch : ") + pitch
anchors.margins: 0
anchors.top: roll.bottom
anchors.left: roll.left
}
}


Here is what I tryied in order to link my cpp values to the qml code.
myTilt is a QQuickItem.

void OpenGLManager::build_qml(QQuickWidget *tilt_qml) {
myTilt = tilt_qml->rootObject();

myTilt->setProperty("yaw", tilt[0]);
myTilt->setProperty("pitch", tilt[1]);
myTilt->setProperty("roll", tilt[2]);
}

void OpenGLManager::setDroneTilt(QList<double> yaw, QList<double> pitch, QList<double> roll)
{
tilt = QVector3D(10, 20, 30) ;
myTilt->setProperty("yaw", tilt[0]);
myTilt->setProperty("pitch", tilt[1]);
myTilt->setProperty("roll", tilt[2]);
}


I'm loading this qml file as source for a QQuickWidget designed in QtDesigner.
The rectangle displays fine but instead of the values I want, I get NaN for Math.abs(yaw) and QQuickText(0x2a29100) for roll and pitch.

I'm using Qt5.6

Answer
    Text {
        id: yaw
        text: qsTr("Yaw : ") + Math.abs(yaw)
        anchors.margins: 5
        anchors.top: tilt.bottom
        anchors.left: tilt.left
    }

I wonder what can go wrong...

It's an ID of the text element.

Also, there is a general practice: never use QML objects from C++ code. So, don't do things like myTilt = tilt_qml->rootObject(); and myTilt->setProperty(...);. Create a C++ class instead and make it usable from QML.

Comments