What is the difference between QQmlApplicationEngine and QQuickView?

Stefan Monov picture Stefan Monov · Oct 20, 2016 · Viewed 12.2k times · Source

I'm using QQmlApplicationEngine as follows:

QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

app.exec();

But now I want to enable multisampling for my app, and QQmlApplicationEngine doesn't seem to have a setFormat method for enabling multisampling.

I found a way to do it with a QQmlApplicationEngine in a forum:

QQuickWindow* window = (QQuickWindow*) engine.rootObjects().first();
QSurfaceFormat format;
format.setSamples(16);
window->setFormat(format)

But it relies on the first root object of the engine being a QQuickWindow, which is not documented in Qt docs. So I don't want to use that technique.

Another way would be to skip QQmlApplicationEngine and create a QQuickView instead. This does have a setFormat method letting me enable multisampling, but I'm wondering, am I losing anything by switching from QQmlApplicationEngine to QQuickView?

In other words, what are the differences between these two classes?

One difference I found is this (from here):

Unlike QQuickView, QQmlApplicationEngine does not automatically create a root window. If you are using visual items from Qt Quick, you will need to place them inside of a Window.

This particular difference doesn't matter to me.

Any other differences?

Answer

Mark Ch picture Mark Ch · Oct 26, 2016

Headline: QQmlApplicationEngine is newer and more powerful than QQuickView.

QQmlApplicationEngine exposes some central application functionality to QML, which QQuickView application would normally control from C++:

  • Connecting Qt.quit() to QCoreApplication::quit()
  • Automatically loads translation files from an i18n directory adjacent to the main QML file.
  • Automatically sets an incubation controller if the scene contains a QQuickWindow.
  • Automatically sets a QQmlFileSelector as the url interceptor, applying file selectors to all QML files and assets.

Ref: Qt docs

At the time when QQmlApplicationEngine was introduced, the Qt Blog had this to say:

In Qt 5.0 we generally created Qt Quick applications by declaring a QQuickView in C++ and setting the base url on it. The drawback of that approach is that you have to use C++ to set properties like width, height etc. In Qt 5.1 we encourage using Window or ApplicationWindow as the root item of your application, giving complete control to Qt Quick, so we are now introducing the QQmlApplicationEngine to make this use case a little bit simpler. The QmlApplicationEngine is all you need to set up your qt quick window, pick up the right translation files and it implicitly connects the application quit() signal to your root window.

Qt Quick Controls 2.0 is able to make use of this extra application control, through the new item ApplicationWindow, which:

  • is similar to the regular QQuickWindow, but adds support for setting a window specific MenuBar, ToolBar and StatusBar in QML.
  • makes it convenient to add a header and footer item to the window.
  • makes it possible to control the window's properties, appearance and layout from QML.
  • supports popups via its overlay property, which ensures that popups are displayed above other content and that the background is dimmed when a modal popup is visible.

So, in order to use some of the Qt Quick Controls features like MenuBar and Popup, we need to:

  • use ApplicationWindow as our top-level QML item instead of Rectangle or Item
  • use the new QQmlApplicationEngine to load the QML from C++ instead of the old QQuickView.