I thought, that it could be a good idea to write a short post how to port an existing Qt4 QML application to Qt5. At first, if you are not familiar how Qt will change for the next version, you should read this doc: The Road to Qt 5. The one of the biggest changes is happening under to hood of QML / QtDeclarative module. As Lars Knoll, wrote in the document, there is a new scene graph, which will replace the old QGraphicsView/QPainter based implementation. So basically what this means is that the gap between QML and HW gets smaller. In other words, in Qt5 you will get a kick ass performance for your QML applications plus extra features like shaders. Yes, you can also use shaders in your QML code!
Qt Modularization
I won’t go to the details here, but another big change is Qt modularization. If you don’t know what’s happening there here’s the picture:
NOTE: The picture above is already quite old, so I noticed that it’s missing one important thing: QtWidgets. QtWidgets can be found under the QtBase/src/widgets. In practice this means, that if you want to include QtWidgets to your application you need to remember to add “widgets” module to the pro file.
For more details about the modularization, you can read this Qt Labs blog post: http://labs.qt.nokia.com/2011/01/21/status-of-qt-modularization/ and check out the repositories: http://qt.gitorious.org/qt.
One of the ideas behind the modularization is that you can pick easier, which modules you want to compile in and to leave out all the unnecessary modules.
Steps to Port Qt4 QML Application to Qt 5
- Get and build Qt5. Follow the instructions: http://developer.qt.nokia.com/wiki/Building_Qt_5_from_Git
- Pick your favorite Qt4 QML project
- Find and replace all ”import QtQuick 1.0″ to “import QtQuick 2.0″ (in QML files)
- Modify your C++ main.cpp to use new Qt 5 specific classes as shown below. (QGuiApplication is not yet documented, but for QQuickView you can find the documentation.)
#include <QGuiApplication>
#include <QQuickCanvas>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setResizeMode(QQuickView::SizeViewToRootObject);
view.setSource(QUrl::fromLocalFile("qml/Qt5test/main.qml"));
view.show();
return app.exec();
}
- If you are using only QML, you need to modify pro file to contain only one module: QT += declarative
- If you want to use Qt Widgets or other Qt modules make sure to add them to the line: Qt += declarative widgets other_modules
- Make sure to use qmake from Qt5′s install directory
- Build and run your app!
That’s it. I think it was quite a small effort to switch from Qt4 to Qt5 and port a QML application. Once again, I can say well done Trolls!
