Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

C++ QML Integration Demo App

 #include <QApplication>
 #include <FelgoApplication>

 #include <QQmlApplicationEngine>

 // include qml context, required to add a context property
 #include <QQmlContext>

 // include custom classes
 #include "src/myglobalobject.h"
 #include "src/myqmltype.h"
 // Uncomment this line to add Felgo Hot Reload and use hot reloading with your custom C++ code
 //#include <FelgoHotReload>

 int main(int argc, char *argv[])
 {

   QApplication app(argc, argv);

   FelgoApplication felgo;

   QQmlApplicationEngine engine;
   felgo.initialize(&engine);

   // Set an optional license key from project file
   // This does not work if using Felgo Developer App, only for Felgo Cloud Builds and local builds
   felgo.setLicenseKey(PRODUCT_LICENSE_KEY);

   // use this during development
   // for PUBLISHING, use the entry point below
   felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));

   // use this instead of the above call to avoid deployment of the qml files and compile them into the binary with qt's resource system qrc
   // this is the preferred deployment option for publishing apps to the app stores, because then your qml files and js files are protected
   // to avoid deployment of your qml files and images, also comment the deploy_resources command in the CMakeLists file
   // also see the CMakeLists.txt file for more details
   // felgo.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));

   // add global c++ object to the QML context as a property
   MyGlobalObject* myGlobal = new MyGlobalObject();
   myGlobal->doSomething("TEXT FROM C++");
   engine.rootContext()->setContextProperty("myGlobalObject", myGlobal); // the object will be available in QML with name "myGlobalObject"

   // register a QML type made with C++
   qmlRegisterType<MyQMLType>("com.yourcompany.xyz", 1, 0, "MyQMLType"); // MyQMLType will be usable with: import com.yourcompany.xyz 1.0

   engine.load(QUrl(felgo.mainQmlFileName()));

   // to start your project with Felgo Hot Reload, comment (remove) the lines "felgo.setMainQmlFileName ..." & "engine.load ...",
   // and uncomment the line below
   //FelgoHotReload felgoHotReload(&engine);
Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded