1. qmlRegisterType 可以注册类型到qml中使用的,尽量注册qml类型,在qml中生成对象由qmlengine管理生命周期。但是这样也有缺陷,就是在其他qml中访问不方便
qmlRegisterUncreatableType<Tester>("Logic", 1, 0, "Tester", "");
qmlRegisterType<AudioTesterList>("Logic", 1, 0, "AudioTesterList");
2. rootContext()->setContextProperty. 直接注册C++对象到qml中使用,这样在同一个engine中加载的qml很方便引用。缺点是写起来很快,但是不方便维护,可能会出现一个对象被四处引用的问题。
AudioTestManager audio_test_manger;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(
"audioDeviceManager", QVariant::fromValue(&audio_test_manger));
3. 设置自适应高分屏:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
4. 设置全局字体:
QGui