What is the difference between QQmlApplicationEngine and QQuickView?
Asked Answered
K

2

32

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?

Knop answered 20/10, 2016 at 11:37 Comment(3)
Depends on what your main.qml looks like.Klopstock
You don't have to assume the root object is a QQuickWindow, you only need a QWindow. You can access it with app.topLevelWindows().first() after loading your qml file, no need to cast or get the root object.Paragrapher
You will be losing convenience functionality provided from QQmlApplicationEngine and ApplicationWindow. Before it was introduced, Creator provided a QQuickView based backbone for qml apps.Twentyfour
S
35

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.
Seat answered 26/10, 2016 at 20:24 Comment(0)
C
11

You can use both together if you don't want your top level item to be a Window.

QQmlApplicationEngine engine;
QQuickView view(&engine, 0);
// your usual engine code
view.show();
Centreboard answered 22/10, 2016 at 16:9 Comment(2)
Above code doesn't work for me and give me: " cannot convert argument 1 from 'QQmlApplicationEngine *' to 'QWindow *' "Byrn
right, this constructor requires two arguments, will editCentreboard

© 2022 - 2024 — McMap. All rights reserved.