qml qt 通信

#ifndef __QMLCPPINTERFACE_H__
#define __QMLCPPINTERFACE_H__
#include <QObject>
#include <QQueue>
#include "MqttManager/MqttManager.h"
class QmlCppInterface : public QObject

{
    Q_OBJECT

public:
    explicit QmlCppInterface(QObject *parent = 0);
    Q_INVOKABLE QString getAttitude();
    Q_INVOKABLE float getAttitudes(int id, int index);
    // Q_INVOKABLE
    static QmlCppInterface *instance;
signals:

    void attitudeChange();

public slots:

    void setAttitude(float x, float y, float z);
    void startMqtt();

private:
    QQueue<float> attitudes[3];
    QString attitudeData;
    MqttManager &mqttManager = MqttManager::instance;
};
#endif // __QMLCPPINTERFACE_H__

中转类


import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
import QtQuick.Dialogs 1.2
//import PathPainter 1.0
import pa.qmlTypes 1.0
Window {
    id:window
    visible: true
    width: 320
    height: 480
    minimumWidth:width
    maximumWidth:width
    minimumHeight:height
    maximumHeight:height
    title: qsTr("Hello World")

    Column {
        id: column
        anchors.fill: parent
        padding: 10

        Row {
            id: row
            height: 200

            Text {
                id: attiText
                text: qsTr("陀螺仪姿态")
                font.pixelSize: 12
            }

        }
    }
    QmlCppInterface{
        id:qmlCppInterface
        onAttitudeChange:{
            attiText.text=qmlCppInterface.getAttitude()
//            canvas.j=canvas.j+1;
//            canvas.j=canvas.j%3;
            canvas.requestPaint();
        }
    }
//    PathPainter{
//        id:pathPainter
//        x: 10
//        y: 10
//        contentsScale: 1
//        anchors.fill: parent
//    }

    FileDialog {
        id:fds
        title: "选择文件"
        folder: shortcuts.desktop
        selectExisting: true
        selectFolder: false
        selectMultiple: false
        nameFilters: ["图像文件(*.gif;*.jpg;*.jpeg;*.png;)"]
        onAccepted: {

            //            labels.text = fds.fileUrl;
            console.log("You chose: " + fds.fileUrl);
//            pathPainter.transmitImagePath(fds.fileUrl)
            viewImage.source=fds.fileUrl
            viewImage.visible=true
            button.anchors.verticalCenter=undefined
            button.anchors.bottom= button.parent.bottom
            picPathText.text=fds.fileUrl
        }

        onRejected: {
        }

    }

    Button {
        id: button
        x: 246
        y: 416
        text: qsTr("链接")
        onClicked: {
            qmlCppInterface.startMqtt()
        }
    }
    Canvas{
            id:canvas
            width: 300
            height: 300
            anchors.centerIn: parent
            property int j: 0
//            onJChanged: canvas.requestPaint()
            onPaint: {
                var ctx = getContext("2d");

//                ctx.save();
//                ctx.strokeStyle = "#148014";
//                ctx.lineWidth = 10
//                ctx.strokeRect(0,0,width,height);

                ctx.beginPath();
                ctx.moveTo(0,0);
//                if(j==0){
                    ctx.clearRect(0,0,width,height);
//                    ctx.restore();
                    ctx.strokeStyle = "#FF7F50";
                    ctx.moveTo(0,qmlCppInterface.getAttitudes(0,0)/2);
                    for(var i=1;i < 299;i+=1)
                    {
                        ctx.lineTo(i,qmlCppInterface.getAttitudes(0,i)/2);
                    }
                    ctx.stroke();
//                    j=j+1;
//                    canvas.requestPaint();
//                }
//                else if(j==1){
    //                ctx.restore();
                    ctx.beginPath();
                    ctx.strokeStyle = "#00BFFF";
                    ctx.moveTo(0,150+qmlCppInterface.getAttitudes(1,0)/2);
                    for(var i=1;i < 299;i+=1)
                    {
                        ctx.lineTo(i,150+qmlCppInterface.getAttitudes(1,i)/2);
                    }
                    ctx.stroke();
//                    j=j+1;
                    canvas.requestPaint();
//                }
//                else if(j==2){
    //                ctx.restore();
                    ctx.beginPath();
                    ctx.strokeStyle = "#148014";
                    ctx.moveTo(0,150+qmlCppInterface.getAttitudes(2,0)/2);
                    for(var i=1;i < 299;i+=1)
                    {
                        ctx.lineTo(i,150+qmlCppInterface.getAttitudes(2,i)/2);
                    }
                    ctx.stroke();
//                }





//                j=j+1;
//                ctx.fillStyle = "green"
//                ctx.fill();
            }

//            Timer{
//                repeat: true
//                interval: 20
//                running: true
//                onTriggered: {
//                    canvas.j+=5;
//                    canvas.requestPaint();
//                }
//            }
        }

}

qml


void registQmlTypes()
{
    qmlRegisterType<QmlCppInterface>("pa.qmlTypes", 1, 0, "QmlCppInterface");
}

注册

image-20201227080935706

© 2021 hanbaoaaa record.浙ICP备20005263号
asdad
联系方式 asdasd
2021-5-8 4:19
sss
回复数 (0) 点击展开
加载更多

新增评论

称呼
联系方式
邮箱(选填)
内容

提交

取消