如何在 QML Shapes 上启用抗锯齿功能? [英] How do I enable antialiasing on QML Shapes?

查看:84
本文介绍了如何在 QML Shapes 上启用抗锯齿功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
                    }

                }
            }
        }
    }
}

由于 antialiasing: true 在 Control 和 Shape 上都设置了,我希望路径看起来很平滑.但是,它看起来参差不齐:

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:

我怎样才能对形状进行抗锯齿处理?

How can I antialias the shape?

推荐答案

根据关于 QtQuick Shapes 的博文,您需要在整个场景或 QtQuick 图层上启用多重采样.

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

您似乎在使用 QQmlApplicationEngine,在这种情况下,您可以简单地在 main.cpp 中设置默认表面格式:

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

或者,如果您希望将此设置限制为单个 QtQuick 层,您也可以像这样设置样本数:

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 ...
    }
}

在图层上设置它对我来说似乎被破坏了 vendorExtensionsEnabled: true 在形状上,这是默认设置.将其设置为 false 似乎可以工作.

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.

这篇关于如何在 QML Shapes 上启用抗锯齿功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆