这是一个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
}
}
}
}
}
}
由于在控件和Shape上都设置了
antialiasing: true
,因此我希望路径看起来很平滑。但是,它看起来参差不齐:我如何抗锯齿形状?
最佳答案
根据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 ...
}
}
在默认情况下,在Shape上使用
vendorExtensionsEnabled: true
对我来说,将其设置在图层上似乎很麻烦。将其设置为false
似乎可以工作。关于qt - 如何在QML Shapes上启用抗锯齿?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48895449/