Qml学习(一):heartrate-game

该项目是Qt示例QtBluetooth模块。

首先看main.cpp:从main函数开始:

int main(int argc, char *argv[])
{
    //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
    QGuiApplication app(argc, argv);

    ConnectionHandler connectionHandler;
    DeviceHandler deviceHandler;
    DeviceFinder deviceFinder(&deviceHandler);

    qmlRegisterUncreatableType("Shared", 1, 0, "AddressType", "Enum is not a type");

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("connectionHandler", &connectionHandler);
    engine.rootContext()->setContextProperty("deviceFinder", &deviceFinder);
    engine.rootContext()->setContextProperty("deviceHandler", &deviceHandler);

    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

    return app.exec();
}

QGuiApplication封装了有关应用程序实例的相关信息(比如程序名,命令行参数等)。

QGuiApplication app(argc, argv);

初始化连接管理模块,设备管理模块,发现设备模块。

    ConnectionHandler connectionHandler;
    DeviceHandler deviceHandler;
    DeviceFinder deviceFinder(&deviceHandler);

QQMlApplicationEngine管理带有层次结构的上下文和组件。

QQmlApplicationEngine engine;

SetContextProperty()用于QML上下文设置上下文属性。

engine.rootContext()->setContextProperty("connectionHandler", &connectionHandler);
engine.rootContext()->setContextProperty("deviceFinder", &deviceFinder);
engine.rootContext()->setContextProperty("deviceHandler", &deviceHandler);

扩展QML常用的三种方式:

1.上下文属性:setContextProperty()

2.向引擎注册类型:在main.cpp调用qmlRegisterType

3.QML扩展插件

QQmlApplicationEngine需要一个QML文件,将其加载作为应用程序的入口点。

engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

 

你可能感兴趣的:(#,QML,QT)