如何在QML图形中启用抗锯齿?

14

以下是一个包含旋钮控件和自定义图形的QML文件:

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,因此我希望路径看起来很平滑。 但是,它看起来很粗糙:

表盘和自定义形状

如何抗锯齿形状?

2个回答

28
根据关于QtQuick Shapes的博客文章,您需要在整个场景或QtQuick图层上启用多重采样。
您似乎正在使用QQmlApplicationEngine,在这种情况下,您可以在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层,则可以像这样设置样本数:

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似乎可以解决。


这是非常有用的输入!以前,我直接在创建引擎后在窗口上设置了表面格式。 QSurfaceFormat format; format.setSamples( 8 ); auto window = dynamic_cast( engine.rootObjects().first() ); window->setFormat( format );然而,在迁移到Qt 5.12之后,这种方法不再起效,显然需要在实例化引擎之前进行设置。 - Tobias Gurdan
1
我正在尝试在Qt 5.15中使其工作,但这两种方法都没有对我的形状产生任何影响 :-( 我还尝试了禁用供应商扩展,但没有运气。仍然很粗糙。 - LubosD
@LubosD,它确实有效。就在几分钟前,我使用它来改善图形质量。请看这里 - user6283344
@EmonHaque 但是你没有使用任何Shape/ShapePath/PathPolyline组件,对吗?我甚至尝试了最简单的情况,但仍然没有发生抗锯齿。 - LubosD
@LubosD,你看到的所有图标(A、B、C、J、K和L)都是放在Shape{ShapePath{PathSvg{path: string}}}中的string - user6283344

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接