Using QmlView

Normally you can use the qmlviewer application to launch your qml scenes. Another way is to use the C++ class QmlView to load and show the scene. In this post, we write our own QmlViewer application.

QmlView is Qt’s C++ class to display qml scenes. It is basically a QGraphicsView, which itself is a QWidget. So it can be used in your application as any other widget from Qt.

The QmlView loads a qml files via an url and shows the content on the display. Sounds simple, here is the code:

#include <QtGui>
#include <QtDeclarative>

int main(int argc, char** argv)
  QApplication app(argc, argv);
  QmlView *view = new QmlView();
  return app.exec();

You need to enable the QtDeclarative module in your .pro file ( QT += declarative )

Note: If you are working on a Mac ensure you have added CONFIG -= app_bundle to the .pro file. Otherwise the viewer will not find the qml file.

The HelloWorld.qml is a simple hello world qml file, which was used in an earlier post. As the file is specified as an url the file could also be somewhere on the network.

To get more control about loading and also to be able to instantiate own QML Components from C++ you can use the QmlEngine (QmlView::engine()).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s