0
点赞
收藏
分享

微信扫一扫

QML用Q_INVOKABLE调用C++的方法

洲行 2022-10-31 阅读 132


举个例子来陈述这个问题:

1、写一个C++方法

class A: public B

{
Q_OBJECT
public:
A(QQuickItem *parent = 0);
~A();
Q_INVOKABLE void init(); //注意这里我们用到了Q_INVOKABLE这个宏

}

2、注册这个类

qmlRegisterType<A>("A", 1, 0, "A");

3、QML中调用

import QtQuick 2.5
import A1.0

Rectangle {
color: "transparent"
property alias painter: painter

Component.onCompleted: {
painter.penColor = "red"
painter.init()
}

A{
id: painter;
onWidthChanged: {
painter.init()
}
}
}

 

举报

相关推荐

0 条评论