qml rectangle with customized rounded borders

huangapple go评论68阅读模式
英文:

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

qml rectangle with customized rounded borders

而普通的带有非常细边框的圆角矩形如下所示:

Rectangle {
    anchors.centerIn: parent
    width: 300
    height: 300
    radius: 30
    border.width: 5
}

qml rectangle with customized rounded borders

我尝试了一些共享属性的调整,但没有帮助,比如 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
}
}
}
}
}

qml rectangle with customized rounded borders

where else Normal rectangle with rounded corners with very fine borders.

Rectangle {
anchors.centerIn: parent
width: 300
height: 300
radius: 30
border.width: 5
}

qml rectangle with customized rounded borders

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;

然后,您可以调整您的ShapeItem.antialiasingItem.smooth属性。

此外,我建议编写一个自定义项目,使用QQuickItem派生,以Rectangle源代码作为示例。

以下是两张图像:带有8个样本的您的Shapesantialiasing: truesmooth: true,以及具有默认设置的普通Rectangle

qml rectangle with customized rounded borders


<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:

qml rectangle with customized rounded borders

huangapple
  • 本文由 发表于 2023年4月4日 10:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定