版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39808061/article/details/119489813
————————————————
版权声明:本文为CSDN博主「某样仙儿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39808061/article/details/119489813
1.定义一个C++类
/* H文件 */
class test_cpp : public QObject
{
Q_OBJECT
QML_ELEMENT
public:
explicit test_cpp(QObject *parent = nullptr);
signals:
void sendSignal(int x);
public slots:
void getSignal(int x);
};
/* C 文件 */
test_cpp::test_cpp(QObject *parent) : QObject(parent)
{
}
void test_cpp::getSignal(int x)
{
qDebug()<<"getSignal"<<x;
emit sendSignal(x);
}
2.实例化并注册到QML文件中
test_cpp test_temp;
qmlRegisterSingletonInstance("test_cpp",1,0,"Test",&test_temp);
3.QML文件使用函数和槽
Test.getSignal(clickNum)
4.QML文件使用信号
Connections{
target: Test
function onSendSignal(x){
console.log("getSignal"+x)
}
}