BlueRoseNote/07-Other/Qt/QtQuick/c++与QML混合编程笔记.md
2023-06-29 11:55:02 +08:00

23 lines
989 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 信号与槽
信号与槽都可以在qml中访问
### Q_INVOKABLE宏
在定义一个类的成员函数时使用Q_INVOKABLE宏来修饰就可以让该方法被元对象系统调用。也就是注册到元对象系统中
例如:
```
Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm);
```
### Q_ENUMS宏
使用Q_ENUMS(枚举名)的方式来注册枚举类型
### Q_PROPERTY宏
Q_PROPERTY宏用来定义可通过元对象系统访问的属性通过它定义的属性可以在QML中访问、修改也可以通过在属性变化时发射特定信号。
### 注册一个QML可用类型
1. 实现c++类
2. 注册QML类型
3. 在QML中导入类型
4. 在QML中创建由c++导出的类型的实例并使用
#### 注册QML类型
1. qmlRegisterSingletonType() 用来注册单例类型
2. qmlRegisterType() 注册非单例类型
3. qmlRegisterTypeNotAvailable() 注册一个类型用来占位
4. qmlRegisterUncreateableType() 注册具有附加属性的附加类型