QT c++ 发送信号到 QML 槽函数 踩过的坑

QT c++ 发送信号到 QML 槽函数

QT c++ 发送信号到 QML 槽函数 踩过的坑

困惑多天的qt向qml中发送信号的坑,我给踩过来了。做个记录分享一下,也许可以帮助其他同学。
直接上代码,请注意代码中有标准序号注释。

QT C++代码内容

#include "main.h"

FaceAnalysis faceAnalysis;  /// c++ 中的信号


int main(int argc, char* argv[])
{
    ///> 1). 注册组建,共享给 qml    
    qmlRegisterType<FaceAnalysis>("FaceAnalysisThread.h",1,0,"FaceAnalysis");

    QGuiApplication app(argc,argv);
    QQuickView view;
    view.setSource(QUrl(QLatin1String("qrc:///main.qml")));
    view.setResizeMode(QQuickView::SizeRootObjectToView);

    QUrl fileName;
    qreal volume = 0.5;
    QQuickItem *rootItem = view.rootObject();
    rootItem->setProperty("fileName",fileName);
    rootItem->setProperty("volume",volume);

    ///> 2). findChild 在qml中定义的objectName
    QObject *obj = rootItem->findChild<QObject*>("debugSignal");
    ///> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
    if (obj){
    ///> 3).  连接       c++ 中信号         (analysisResultNotify)   至 qml中信号(testSignal)
       QObject::connect(&faceAnalysis,SIGNAL(analysisResultNotify()),obj,SIGNAL(testSignal()));
       qDebug() << " === initialize testSignal user == " ;
    }
   
    //! \note configure view parament
    view.setTitle("<< 人 脸 识 别 >>");
    view.setResizeMode(QQuickView::SizeRootObjectToView);
    view.resize(640, 480);
    view.show();

    // Delay invocation of init until the event loop has started, to work around
    // a GL context issue on Harmattan: without this, we get the following error
    // when the first ShaderEffectItem is created:
    // "QGLShaderProgram::addShader: Program and shader are not associated with same context"
    QMetaObject::invokeMethod(view.rootObject(), "init", Qt::QueuedConnection);

    return app.exec();
}

下面QML的代码,camera采集视频。

import QtQuick 2.9
import QtQuick.Window 2.1
import QtMultimedia 5.5
import QtQuick.Controls 2.2

///> 1). 导入main.cpp中注册组建 
import FaceAnalysisThread.h 1.0

Rectangle {
    id:cameraUI

    visible: true
    width: 640
    height: 480
    
    color: "black"
    state:"PhotoView"

    Text{
        id:text
        objectName: "debugSignal"  ///> 2). qml 定义的 objectName,通过元系统共享给main.cpp
        /// ^^^^^^^^^^^^^^^^^^^^^
        
        text:"hello"
        visible: true
        signal testSignal();      ///> 3).  qml 中定义的信号(testSignal),main.cpp中connet关联的信号
        /// ^^^^^^^^^^^^^^^
        onTestSignal: {           ///> 4).  qml中定义信号(testSignal)的槽函数(onTestSignal)
        //! ^^^^^^^^^^^^^^^
            console.log("hello qml slot function");
        }

    }

    Camera {
        id:camera
        captureMode: Camera.CaptureStillImage
        //viewfinder.resolution:"160x120"
        //viewfinder.resolution:"320x240"
        viewfinder.resolution:"640x480"

        imageCapture {
            id: cameracapture
            onImageCaptured: {
                camera.stop();
                //var imgPath = camera.imageCapture.capturedImagePath;
                camera.start();

            }

            onImageSaved:{
                var path = camera.imageCapture.capturedImagePath;
                cameraUI.qmlPhotoNotifySignal(path);
                
                var p = faceAnalysis.getAnalysisResult();   ///> 路线第二站 camera 图片捕获槽函数,触发c++文件中的信号
                ///> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                console.log("image path: " + p);
            }
        }

        Component.onCompleted: {
            var resolution = camera.viewfinder.resolution;
            console.log("resolution: " + resolution.width + " " + resolution.height);
            console.log("deviceId: " + camera.deviceId)
        }
        onCameraStateChanged: {
            //console.log("camera state changed to: " );
        }
    }

    Item {
        id:itemVideo
       // width: 320
        //height: 240
       width: parent.width
       height: parent.height

        VideoOutput {
            id:viewfinder
            source: camera
            anchors.fill: parent
            visible: camera.imageCapture.ready
            focus : visible // to receive focus and capture key events when visible
            MouseArea {
                id:video_click
                anchors.fill: viewfinder
                visible: camera.imageCapture.ready
                onClicked: {
                    camera.imageCapture.capture();    ///> 事件首发路线起点,点击VideoOutput界面,触发camera图片捕获槽函数
                }
            }
        }
    }

    function init(){//c++ invokeMethod this Function
        if (Qt.platform.os == "linux" || Qt.platform.os == "window"){
            if( Screen.desktopAvailableWidth > 1280){
                windowWidth = 1280;
            }
            if(Screen.desktopAvailableHeight > 720){
                windowHeight = 720;
            }
        }
        console.debug("H:%d,W%d",windowHeight,windowWidth);
        //performanceLoader.init();
        console.debug("qml initialize startup...");
    }

}

工程代码参考github:https://github.com/dtltljb/Qt-face-detect

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值