Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable antialiasing on QML Shapes?

Tags:

qt

qml

Here's a QML file that has a Dial control and a custom shape side by side:

import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import QtQuick.Window 2.2
import QtQuick.Shapes 1.0


Window {
    id: window
    visible: true
    width: 400
    height: 200

    RowLayout {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        spacing: 5

        Dial {
            id: dial1
        }

        Control {
            id: dial2

            implicitWidth: dial1.width
            implicitHeight: dial1.height

            antialiasing: true

            Shape {
                anchors.fill: parent

                antialiasing: true

                ShapePath {
                    strokeWidth: 1
                    strokeColor: dial2.visualFocus ? dial2.palette.highlight : dial2.palette.dark

                    startX: dial2.width/2
                    startY: 0

                    PathArc {
                        x: dial2.width/2
                        y: dial2.height
                        radiusX: dial2.width/2
                        radiusY: dial2.height/2
                        direction: PathArc.Clockwise
                    }

                    PathArc {
                        x: dial2.width/2
                        y: 0
                        radiusX: dial2.width/2
                        radiusY: dial2.height/2
                        direction: PathArc.Clockwise
                    }

                }
            }
        }
    }
}

Since antialiasing: true is set on both the Control and the Shape, I would have expected the path to look smooth. However, it looks jagged:

Dial and custom shape

How can I antialias the shape?

like image 238
detly Avatar asked Feb 20 '18 22:02

detly


1 Answers

According to the blog post about QtQuick Shapes, you need to either enable multisampling on the whole scene or on a QtQuick layer.

You appear to be using a QQmlApplicationEngine, in which case you can simply set the default surface format in main.cpp:

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

    QSurfaceFormat format;
    format.setSamples(8);
    QSurfaceFormat::setDefaultFormat(format);

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

    return app.exec();
}

Alternatively, if you prefer to restrict this setting to a single QtQuick layer, you can also set the number of samples like this:

import QtQuick 2.8
import QtQuick.Window 2.2
import QtQuick.Shapes 1.0

Window {
    visible: true
    width: 640
    height: 480

    Item {
        id: root
        anchors.fill: parent
        layer.enabled: true
        layer.samples: 4
        // your shapes here ...
    }
}

Setting it on a layer appears to be broken for me with vendorExtensionsEnabled: true on the Shape, which is the default. Setting it to false appears to work.

like image 116
dragly Avatar answered Sep 22 '22 06:09

dragly