英文:
qml rectangle with customized rounded borders
问题
以下是您要翻译的部分:
"So there was need of qml rectangle with customized rounded corners. I ended up crating one with shapes, pathLines, pathArcs and bit of math.
Somehow my arcs/rounded corners are not sharp/fine as Rectangle with radius property.
How cam I make those corners more fine ?
Item {
anchors.centerIn: parent
id: root
width: 300
height: 300
property string color: 'red'
property int rightTopCornerRadius: 10
property int rightBottomCornerRadius: 20
property int leftBottomCornerRadius: 30
property int leftTopCornerRadius: 40
Shape {
ShapePath {
strokeWidth: 5
strokeColor: root.color
startX: root.leftTopCornerRadius > 0 ? root.leftTopCornerRadius : 0
startY: 0
fillColor: "transparent"
capStyle: ShapePath.RoundCap
fillRule: ShapePath.WindingFill
PathLine { y: 0; x: root.width - root.rightTopCornerRadius }
PathArc {
x: root.width; y: root.rightTopCornerRadius
radiusX: root.rightTopCornerRadius; radiusY: root.rightTopCornerRadius
}
PathLine { x: root.width; y: root.height - root.rightBottomCornerRadius }
PathArc {
x: root.width - root.rightBottomCornerRadius; y: root.height
radiusX: root.rightBottomCornerRadius; radiusY: root.rightBottomCornerRadius
}
PathLine { x: root.leftBottomCornerRadius; y: root.height }
PathArc {
x: 0; y: root.height - root.leftBottomCornerRadius
radiusX: root.leftBottomCornerRadius; radiusY: root.leftBottomCornerRadius
}
PathLine { x: 0; y: root.leftTopCornerRadius }
PathArc {
x: root.leftTopCornerRadius; y: 0
radiusX: root.leftTopCornerRadius; radiusY: root.leftTopCornerRadius
}
}
}
}
而普通的带有非常细边框的圆角矩形如下所示:
Rectangle {
anchors.centerIn: parent
width: 300
height: 300
radius: 30
border.width: 5
}
我尝试了一些共享属性的调整,但没有帮助,比如 joinstyle、capstyle、fillrule。"
英文:
So there was need of qml rectangle with customized rounded corners. I ended up crating one with shapes, pathLines, pathArcs and bit of math.
Somehow my arcs/rounded corners are not sharp/fine as Rectangle with radius property.
How cam I make those corners more fine ?
Item {
anchors.centerIn: parent
id: root
width: 300
height: 300
property string color: 'red'
property int rightTopCornerRadius: 10
property int rightBottomCornerRadius: 20
property int leftBottomCornerRadius: 30
property int leftTopCornerRadius: 40
Shape {
ShapePath {
strokeWidth: 5
strokeColor: root.color
startX: root.leftTopCornerRadius > 0 ? root.leftTopCornerRadius : 0
startY: 0
fillColor: "transparent"
capStyle: ShapePath.RoundCap
fillRule:ShapePath.WindingFill
PathLine { y: 0; x:root.width - root.rightTopCornerRadius}
PathArc {
x: root.width; y: root.rightTopCornerRadius
radiusX: root.rightTopCornerRadius; radiusY: root.rightTopCornerRadius
}
PathLine { x:root.width; y:root.height - root.rightBottomCornerRadius}
PathArc {
x:root.width - root.rightBottomCornerRadius; y: root.height
radiusX: root.rightBottomCornerRadius; radiusY: root.rightBottomCornerRadius
}
PathLine { x:root.leftBottomCornerRadius; y:root.height}
PathArc {
x:0; y: root.height - root.leftBottomCornerRadius
radiusX: root.leftBottomCornerRadius; radiusY: root.leftBottomCornerRadius
}
PathLine { x:0; y:root.leftTopCornerRadius}
PathArc {
x:root.leftTopCornerRadius; y: 0
radiusX: root.leftTopCornerRadius; radiusY: root.leftTopCornerRadius
}
}
}
}
}
where else Normal rectangle with rounded corners with very fine borders.
Rectangle {
anchors.centerIn: parent
width: 300
height: 300
radius: 30
border.width: 5
}
I tried playing with some of the share properties, but nothing helped.
like joinstyle, capstyle, fillrule
答案1
得分: 3
好的,有几种方法可以做到这一点。首先,您需要启用QtQuick的多重采样。将以下代码添加到您的main.cpp文件中:
QGuiApplication app(argc, argv);
QSurfaceFormat format; //
format.setSamples(8); // 添加这些行,根据需要更改值
QSurfaceFormat::setDefaultFormat(format); //
QQmlApplicationEngine engine;
然后,您可以调整您的Shape
的Item.antialiasing和Item.smooth属性。
此外,我建议编写一个自定义项目,使用QQuickItem派生,以Rectangle
的源代码作为示例。
以下是两张图像:带有8个样本的您的Shapes
,antialiasing: true
,smooth: true
,以及具有默认设置的普通Rectangle
:
<details>
<summary>英文:</summary>
Ok, there are several options to do that. First of all you have to turn the QtQuick multisampling on. Add the following lines into your main.cpp:
```cpp
QGuiApplication app(argc, argv);
QSurfaceFormat format; //
format.setSamples(8); // add these lines, change the value if needed
QSurfaceFormat::setDefaultFormat(format); //
QQmlApplicationEngine engine;
Then you can play with Item.antialiasing and Item.smooth properties of your Shape
.
Besides that, I would recommend to write a custom item using QQuickItem deriving, taking the Rectangle
sources as example.
Here are 2 images: your Shapes
with 8 samples, antialiasing: true
,
smooth: true
, and a common Rectangle
with default settings:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论