关于qt:如何在QML Shapes上启用抗锯齿?

How do I enable antialiasing on QML Shapes?

这是一个具有Dial控件和自定义形状并排的QML文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
                    }

                }
            }
        }
    }
}

由于在控件和图形上均设置了antialiasing: true,所以我希望路径看起来很平滑。但是,它看起来参差不齐:

Dial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
}

或者,如果您希望将此设置限制为单个QtQuick层,则还可以设置以下样本数量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 ...
    }
}

将其设置在图层上似乎对Shape的vendorExtensionsEnabled: true无效,这是默认设置。将其设置为false似乎可以使用。


您是否尝试过启用平滑功能?我会检查一下,但我目前正在将Qt 4.8用于一个项目,并且没有加载5.x来亲自尝试。

https://doc.qt.io/qt-5.10/qml-qtquick-item.html#smooth-prop