QMLrocks

Compilation

This time, we will see how to compile your application, to have a binary (eg. .exe for Windows) and be able to run it without qmlscene.

To do so, we need to complicate a bit and add some files to your directory.

Add a main.cpp file

Yeah, Qt is a C++ framework, so we still need a small C++ wrapper to rum a QML app.

Create a main.cpp file, and copy the folowing code inside.

#ifndef QT_NO_WIDGETS #include typedef QApplication Application; #else #include typedef QGuiApplication Application; #endif #include #include int main(int argc, char **argv) { Application app(argc, argv); QQmlApplicationEngine appEngine; appEngine.load(QUrl("qrc:/main.qml")); QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load"); return app.exec(); }

Add a .pro file

This file will be used by the qmake command to generate a Makefile.

Here's a sample for our hellowold-qml app, (see Prototyping), let's call it helloworld-qml.pro

QT += quick qml QT += widgets SOURCES += main.cpp RESOURCES += helloworld-qml.qrc

Add a .qrc file

Finally, we need to add a Qt ressources file, to tell the compiler where the files that are used by QML are located.

Create a qml.qrc, and paste the folowing content.

<DOCTYPE RCC> <RCC version="1.0"> <qresource> <file>main.qml</file> </qresource> </RCC>

Compile and run

In your directory you should now have at least 4 files : main.cpp, helloword-qml.pro, qml.qrc, main.qml

Open your command line in this directory and run the folowing commands : 

$ qmake $ make

TADAA, a helloworld-qml binary appeared in your folder ! You can run it.

//IMG