What is the difference between QQmlApplicationEngine and QQuickView?



  • 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?


  • Moderators

    @Stefan-Monov76
    The basic difference is QQmlApplicationEngine can only load QML files which have root component as Window or ApplicationWindow while QQuickView loads any QML type which inherits Item.

    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.

    If you use QQmlApplicationEngine you have to use Window or ApplicationWindow which instantiates QQuickWindow.
    So AFAIK the code you posted is the only way to set the samples.



  • @p3c0 : Thanks. Seems like someone had too much time on their hands and spent it creating redundant classes :)


  • Moderators

    @Stefan-Monov76 As the docs say QQuickView is convenience subclass of QQuickWindow.



  • QQmlApplicationEngine is a convenience class introduced in Qt 5.1, with several advantages over QQuickView:

    • Can also be used for non-graphical purposes (so whether the root object is a QQuickView depends on the QML you're loading).
    • Automates a bunch of things (Qt.quit(), translations, file selectors)
    • Allows subclasses like ApplicationWindow to be used as root item (whereas you would have a Window nested in a Window when using a QQuickView, I'm not sure what it does in that case)

    While technically redundant, I think it was a welcome addition since it saves a lot of boilerplate code.



  • @thorbjorn : Thanks! Very useful answer (unlike the docs, which are really lacking on this :) ). I'll use the QQmlApplicationEngine way.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.