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?


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);
}

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.");
    }
}

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

int main(int argc, char *argv[])
{
    EnableDefaultAA();

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    EnableAA();

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    EnableAntialiasingOnEngine(app);

    return app.exec();
}



/////////// Main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

Window
{
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Column
    {
        spacing: 5

        TriangleButtonDU
        {
            height: 50
            direction: 'up'
        }
        TriangleButtonDU
        {
            height: 50
            direction: 'left'
        }
        TriangleButtonDU
        {
            height: 50
            direction: 'down'
        }
        TriangleButtonDU
        {
            height: 50
            direction: 'right'
        }
    }

}

////////////// TriangleButtonDU.qml
import QtQuick 2.10
import QtQuick.Shapes 1.12

Shape
{
    id: component

    property color color: 'black'
    property color downColor: 'red'
    property alias fillColor: path_.fillColor
    property alias thickness: path_.strokeWidth
    property string direction: 'up' // Support 'up', 'down', 'left', 'right'

    width: height
    transformOrigin: Item.Center
    rotation: directionToDeg(direction)
    smooth: true
    antialiasing: true
     // Also try the layer without success
    //layer.enabled: true
    //layer.samples: 8

    signal clicked(var mouse)

    function directionToDeg(dir)
    {
        switch(dir.toLowerCase())
        {
            case 'left':  return 270;
            case 'down':  return 180;
            case 'right': return 90;
            case 'up':
            default:      return 0;
        }
    }

    ShapePath
    {
        id: path_
        property real sideSpacing: component.thickness / 2
        property real minX: sideSpacing
        property real middleX: component.width / 2
        property real maxX: component.width - sideSpacing
        property real minY: component.thickness
        property real maxY: component.height - sideSpacing
        startX: middleX
        startY: minY
        strokeColor: mouseArea_.pressed ? component.downColor : component.color
        fillColor: 'blue'
        strokeWidth: 4
        joinStyle: ShapePath.RoundJoin
        strokeStyle: ShapePath.SolidLine
        capStyle: ShapePath.RoundCap

        PathLine
        {
            x: path_.minX
            y: path_.maxY
        }
        PathLine
        {
            x: path_.maxX
            y: path_.maxY
        }
        PathLine
        {
            x: path_.startX
            y: path_.startY
        }
    }

    MouseArea
    {
        id: mouseArea_
        anchors.fill: parent
        onClicked: component.clicked(mouse);
    }
}




_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to