QML--将C++的信号绑定到QML的槽函数中

操作如下

① 首先定义好自己的类

② 定义C++处信号

signals:
    void updatePointValue(double point1Value, 
                        double point2Value, 
                        double point3Value);

③ 在QML中将C++的信号绑定到函数

function setPoint(point1Value, point2Value, point3Value)
{
//            console.log("[Info]--enter the setPoint() function\r\n");
	var f1 = [];
	var f2 = [];
	var f3 = [];
	f1[0] = point1Value;
	f2[0] = point2Value;
	f3[0] = point3Value;

	idWaveChart1.addData(f1);
	idWaveChart2.addData(f2);
	idWaveChart3.addData(f3);
}
Component.onCompleted: {
	ControlsInitializer.initQuickRoot(idRoot)
	ControlsInitializer.initTheme("qrc:/themes", "normal")

	console.log("主题列表 == " + JSON.stringify(ThemeController.themeList) 
	+ "\n" + "当前主题 == " + ThemeController.appTheme)
	idSerialport.updatePointValue.connect(idContentRoot.setPoint);
}

④ 当需要将C++的数据发送到QML时,通过emit发出信号即可,信号可以带参数,也可以不带参数

void SerialPort::setNewData()
{
    bool bGetPoint = false;
    uint32_t uiDataLength = 0;
    double pdPointValue[3] = {0};
    uint8_t *pucWaveData = nullptr;
    m_newSerialHexData = m_serial->readAll();

    bGetPoint = serialFormatData->WaveDataProcess(m_newSerialHexData, 
        &pucWaveData, &uiDataLength);
    if(bGetPoint)
    {
        serialFormatData->GetPointValue(pucWaveData, pdPointValue);
        emit updatePointValue(pdPointValue[0], pdPointValue[1], pdPointValue[2]);
    }

    setDataString(m_newSerialHexData.toHex() + "\r\n");
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值