How to create and use C++ objects in QML Javascript

前端 未结 4 588
北海茫月
北海茫月 2021-02-05 21:43

My app uses both c++ and QML.

I\'ve defined several objects in C++ part to access SQL etc.

It looks like:



        
4条回答
  •  不思量自难忘°
    2021-02-05 22:04

    You can use

    QQmlApplicationEngine engine;
    engine.globalObject().setProperty("CppCreator", engine.newQObject(&CppCreator::GetInstance()));
    

    CppCreator is an QObject to create other c++ object

    Q_INVOKABLE QObject* Create(const QString& type_name);
    

    Then you can create c++ object in qml js like

    var test = CppCreator.Create("Your Type");
    

    It is not perfect but satisfied my requirement. Hope it helps you.

提交回复
热议问题