[Interest] QQmlApplicationEngine and antialiasing

Jérôme Godbout godboutj at amotus.ca
Tue Jan 15 16:10:44 CET 2019


Thanks for the tips, yeah This is what I tried first, but I don't see any effect whatsoever. If I read back the QSturfaceFormat, it only display the given samples but not on visual effect seen.

My log trace: Surface Format:  "Version: 3.0 Profile: No Profile Samples: 8 Depth: 24 Alpha: 8 Red: 8 Green: 8 Blue: 8"

What I see:


There is no difference between 0, 2, 4, 8 except the print QSurfaceFormat reported value. Is there anything else I should check? Is OpenGL used at all?

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QSurfaceFormat>
#include <QWindow>
#include <QQuickWindow>
#include <QOpenGLContext>
#include <QDebug>
#include <QStringList>
#include <algorithm>

#define NB_AA_SAMPLE 8

void PrintSurfaceFormat(QSurfaceFormat& format)
    QStringList info;
        << QString("Version: %1.%2").arg(format.version().first).arg(format.version().second)
        << QString("Profile: %1").arg(format.profile() == QSurfaceFormat::CoreProfile
                                        ? "Core"
                                        : format.profile() == QSurfaceFormat::CompatibilityProfile
                                            ? "Compatibility"
                                            : "No Profile")
        << QString("Samples: %1").arg(format.samples())
        << QString("Depth: %1").arg(format.depthBufferSize())
        << QString("Alpha: %1").arg(format.alphaBufferSize())
        << QString("Red: %1").arg(format.redBufferSize())
        << QString("Green: %1").arg(format.greenBufferSize())
        << QString("Blue: %1").arg(format.blueBufferSize())
    qDebug() << "Surface Format: " << info.join(" ");

void CheckOpenGLContext(QOpenGLContext* ogl_contex)
    QSurfaceFormat surface_format = ogl_contex->format();
    if(surface_format.samples() != NB_AA_SAMPLE)
        qWarning("AA sample rate not supported on this machine.");

void CheckOGL(QGuiApplication& app)
    QList<QWindow*> windows = app.topLevelWindows();
    std::any_of(windows.begin(), windows.end(), [&](QWindow* w)
        QQuickWindow* window = qobject_cast<QQuickWindow*>(w);
        if(window != nullptr)
            QObject::connect(window, &QQuickWindow::openglContextCreated, [&](QOpenGLContext* ogl_ctx){ CheckOpenGLContext(ogl_ctx); });
            QOpenGLContext* ogl_context = window->openglContext();
            if(ogl_context != nullptr)
            return true;
        return false;

void EnableDefaultAA()
    QSurfaceFormat surface_format = QSurfaceFormat::defaultFormat();

int main(int argc, char *argv[])

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    if (engine.rootObjects().isEmpty())
        return -1;


    return app.exec();

From: Interest <interest-bounces at qt-project.org> on behalf of Giuseppe D'Angelo via Interest <interest at qt-project.org>
Sent: Monday, January 14, 2019 6:37 PM
To: interest at qt-project.org
Subject: Re: [Interest] QQmlApplicationEngine and antialiasing

On 14/01/2019 22:10, Jérôme Godbout wrote:
> Hi,
> I'm trying to enable the antialiasing inside my project that use
> QQmlApplicationEngine (currently under Linux). I have some QMl Shape
> that look ugly and the antialiasing doesn't seem to work at all. What is
> the proper way to enable the AA that is cross platform and will work on
> both QtCreator debug and release mode?

Note that debug/release mode has nothing to do with this...

Assuming that you're using the OpenGL backend:

> I have try to enable the default surface at the very beginning of the
> main, after the application is created or even on the QWindow surface,
> but nothing seem to work:
> #include <QGuiApplication>
> #include <QQmlApplicationEngine>
> #include <QSurfaceFormat>
> #include <QQuickWindow>
> #include <QOpenGLContext>
> #define NB_AA_SAMPLE 8
> void EnableAntialiasingOnEngine(QGuiApplication& app)
> {
>      QWindow* window = app.topLevelWindows().first();
>      QSurfaceFormat surface_format; // = window->format();
>      surface_format.setSamples(NB_AA_SAMPLE); // AA sampling
>      window->setFormat(surface_format);
> }

This is meaningless; you need to set the format on the window AND the
context and you need to do so before they're created (in the sense of
calling create() on them). In other words, this is incomplete and too late.

> void EnableAA()
> {
>      QOpenGLContext ogl_context;
>      QSurfaceFormat surface_format;
>      surface_format.setSamples(NB_AA_SAMPLE);
>      ogl_context.setFormat(surface_format);
>      if(!ogl_context.create())
>      {
>          qWarning("Cannot create OpenGL context for AA.");
>      }
> }

This might be useful as a check that you CAN create an OpenGL context
that supports MSAA. But note that the mere creation isn't enough, you
need to get the format back from the created context (context.format())
and check *that*.

> void EnableDefaultAA()
> {
>      QSurfaceFormat surface_format = QSurfaceFormat::defaultFormat();
>      surface_format.setSamples(NB_AA_SAMPLE); // AA sampling
>      QSurfaceFormat::setDefaultFormat(surface_format);
> }

This is the correct way and should work, given you call this before
creating QGuiApplication. If the result is still aliased, grab the root
object, downcast it (it's going to be some QQuickWindow subclass), get
its OpenGL context after it has been created (openglContext(), or
connect to openglContextCreated()), and dump its format. Thus double
checking that your implementation DOES support multisampling...

My 2 c,
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190115/cd953f85/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pastedImage.png
Type: image/png
Size: 1853 bytes
Desc: pastedImage.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190115/cd953f85/attachment.png>

More information about the Interest mailing list