先声明c++的类
class Myclass{
.....
signals:
void valueChanged(bool res);
.....
}
Myclass::function(){
...........
emit valueChanged(bool res);
}
myclass类中有一个valueChanged的信号,然后在其他函数中发出这些信号
在main.cpp中将Myclass类注册到qml中
Myclass *control=new Myclass();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("control", control);//注册到qml中的名字是“”部分
然后在qml中把槽和c++中的信号用Connection连接起来
Connections {
target: control //这是注册到qml中的c++类的名字
function onValueChanged(res){ //这里就是信号对应的槽函数的名字,把函数名设置为(on+信号
//名),就能自动识别是哪个信号,括号内是信号传的参数,只说明参
//要做的操作 // 数名(方便在槽函数中使用)不用说明类型
}
}