QML与CPP的交互:属性双元导致NaN

QML interaction with CPP : property double results in NaN

本文关键字:NaN 属性 CPP 交互 QML      更新时间:2023-10-16

这是我的QML文件,它是一个简单的矩形,我想在其中显示从我的cpp代码(偏转,滚动和俯仰)中摘录的双值。

倾斜。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
    }
 }

这是我为了将我的cpp值链接到qml代码所尝试的。myTilt是一个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]);
}

我正在加载这个qml文件作为QtDesigner中设计的QQuickWidget的源。矩形显示良好,但不是我想要的值,我得到NaN Math.abs(偏角)和QQuickText(0x2a29100)的滚动和俯仰。

我使用Qt5.6

文本{id: 偏航text: qsTr("Yaw: ") + Math.abs( Yaw )锚。利润:5锚。上图:tilt.bottom锚。左:tilt.left}之前

我想知道会出什么问题…

text元素的ID。

还有一个通用的做法:永远不要在c++代码中使用QML对象。所以,不要做myTilt = tilt_qml->rootObject();myTilt->setProperty(...);。创建一个c++类,让它可以从QML中使用。