gpt4 book ai didi

qt - 如何在 QML 形状上启用抗锯齿功能?

转载 作者:行者123 更新时间:2023-12-02 02:44:56 25 4
gpt4 key购买 nike

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

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 and custom shape

如何对形状进行抗锯齿处理?

最佳答案

根据the blog post about 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 似乎有效。

关于qt - 如何在 QML 形状上启用抗锯齿功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com