在QML中,如何使“Shape”的“阴影”更漂亮?

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

In qml, how to make the "shadow" of "Shape" more beautiful?

问题

这是你提供的代码的翻译部分:

  1. 我创建了一个选项卡栏的示例:
  2. ![选项卡选择卡片](https://i.stack.imgur.com/Jg3FL.png)
  3. 以下是我的代码:
  4. Item {
  5. id: myItem
  6. property bool active: false
  7. property int customWidth: 200
  8. width: 200 + 115 + 115
  9. height: 72
  10. x: 100
  11. y: 20
  12. containmentMask: myShape
  13. Shape {
  14. id: myShape
  15. y: 8
  16. antialiasing: true
  17. width: 200 + 115 + 115
  18. height: 64
  19. anchors.centerIn: parent
  20. ShapePath {
  21. strokeWidth: 0
  22. fillColor: "white"
  23. startX: 0
  24. startY: 64
  25. PathArc {
  26. x: 30
  27. y: 30
  28. radiusX: 32
  29. radiusY: 32
  30. useLargeArc: false
  31. direction: PathArc.Counterclockwise
  32. }
  33. PathArc {
  34. x: 64
  35. y: 0
  36. radiusX: 32
  37. radiusY: 32
  38. useLargeArc: false
  39. direction: PathArc.Clockwise
  40. }
  41. PathLine {
  42. x: 115
  43. y: 0
  44. }
  45. PathLine {
  46. x: 115 + myItem.customWidth
  47. y: 0
  48. }
  49. PathLine {
  50. x: 115 + myItem.customWidth + 51
  51. y: 0
  52. }
  53. PathArc {
  54. x: 115 + myItem.customWidth + 51 + 34
  55. y: 34
  56. radiusX: 32
  57. radiusY: 32
  58. useLargeArc: false
  59. direction: PathArc.Clockwise
  60. }
  61. PathArc {
  62. x: 115 + myItem.customWidth + 115
  63. y: 64
  64. radiusX: 32
  65. radiusY: 32
  66. useLargeArc: false
  67. direction: PathArc.Counterclockwise
  68. }
  69. PathLine {
  70. x: 0
  71. y: 64
  72. }
  73. }
  74. }
  75. MouseArea {
  76. anchors.fill: parent
  77. anchors.leftMargin: 40
  78. anchors.rightMargin: 40
  79. onClicked: {
  80. console.log("clicked")
  81. }
  82. }
  83. }
  84. DropShadow {
  85. source: myItem
  86. anchors.fill: myItem
  87. radius: 3
  88. horizontalOffset: 0
  89. verticalOffset: 0
  90. color: ReharkUIColor.brand06
  91. spread: 0.2
  92. }

然而,我的代码和阴影的设计之间仍然存在差异。我尝试了不同的方法,但无法解决这个问题,无法使我的阴影与设计一样柔和。

在QML中,如何使“Shape”的“阴影”更漂亮?

  1. <details>
  2. <summary>英文:</summary>
  3. I make a tab bar demo of this:
  4. ![Tambor Choice Card](https://i.stack.imgur.com/Jg3FL.png)
  5. And here is my code:
  6. Item {
  7. id: myItem
  8. property bool active: false
  9. property int customWidth: 200
  10. width: 200 + 115 + 115
  11. height: 72
  12. x: 100
  13. y: 20
  14. containmentMask: myShape
  15. Shape {
  16. id: myShape
  17. y: 8
  18. antialiasing: true
  19. width: 200 + 115 + 115
  20. height: 64
  21. anchors.centerIn: parent
  22. ShapePath {
  23. strokeWidth: 0
  24. fillColor: &quot;white&quot;
  25. startX: 0
  26. startY: 64
  27. PathArc {
  28. x: 30
  29. y: 30
  30. radiusX: 32
  31. radiusY: 32
  32. useLargeArc: false
  33. direction: PathArc.Counterclockwise
  34. }
  35. PathArc {
  36. x: 64
  37. y: 0
  38. radiusX: 32
  39. radiusY: 32
  40. useLargeArc: false
  41. direction: PathArc.Clockwise
  42. }
  43. PathLine {
  44. x: 115
  45. y: 0
  46. }
  47. PathLine {
  48. x: 115 + myItem.customWidth
  49. y: 0
  50. }
  51. PathLine {
  52. x: 115 + myItem.customWidth + 51
  53. y: 0
  54. }
  55. PathArc {
  56. x: 115 + myItem.customWidth + 51 + 34
  57. y: 34
  58. radiusX: 32
  59. radiusY: 32
  60. useLargeArc: false
  61. direction: PathArc.Clockwise
  62. }
  63. PathArc {
  64. x: 115 + myItem.customWidth + 115
  65. y: 64
  66. radiusX: 32
  67. radiusY: 32
  68. useLargeArc: false
  69. direction: PathArc.Counterclockwise
  70. }
  71. PathLine {
  72. x: 0
  73. y: 64
  74. }
  75. }
  76. }
  77. MouseArea {
  78. anchors.fill: parent
  79. anchors.leftMargin: 40
  80. anchors.rightMargin: 40
  81. onClicked: {
  82. console.log(&quot;clicked&quot;)
  83. }
  84. }
  85. }
  86. DropShadow {
  87. source: myItem
  88. anchors.fill: myItem
  89. radius: 3
  90. horizontalOffset: 0
  91. verticalOffset: 0
  92. color: ReharkUIColor.brand06
  93. spread: 0.2
  94. }
  95. But,is still has difference between my code and the design in Shadow.
  96. I have tried different ways,but I can not solve this problem, I can not make my shadow as soft as the design.
  97. ![The Design](https://i.stack.imgur.com/3XWpS.png)
  98. </details>
  99. # 答案1
  100. **得分**: 1
  101. 好的,以下是翻译好的部分:
  102. 所以,我修复了你的形状,它实际上是一团糟。现在形状更通用,更容易调整其效果比例。你还可以改变曲线的宽度。
  103. 要从侧面添加更多阴影,有一个简单的解决方案:&lt;br&gt;
  104. 通过使用*模糊效果*并从形状中心进行*x缩放*,你可以实现所需的结果。
  105. 在这个示例中,我使用了[`MultiEffect`](https://doc.qt.io/qt-6/qml-qtquick-effects-multieffect.html),它是Qt6中的一个多用途组件,有助于将多个效果合并成一个单一的组件。
  106. *如果你使用的是Qt5,建议使用[`FastBlur`](https://doc.qt.io/qt-5/qml-qtgraphicaleffects-fastblur.html)代替投影阴影,以获得更好的性能。*
  107. 你还可以设置`colorizationColor`来改变形状下面阴影效果的颜色。
  108. &gt; **注意:** 在我看来,我不建议使用`Shape`,因为它没有正确实现,并且与其他库如*qnanopainter*或*pure shader*相比,效果非常差劲。
  109. [![阴影预览图][1]][1]
  110. ```qml
  111. component MyShape: Shape {
  112. id: shape
  113. property real halfHeight: height/2
  114. property real curveWidth: height/3
  115. ShapePath {
  116. strokeWidth: 0
  117. fillColor: &#39;#fff&#39;
  118. startX: 0; startY: shape.height
  119. PathQuad { x: curveWidth; relativeY: -halfHeight; relativeControlX: curveWidth; relativeControlY: 0}
  120. PathQuad { x: 2 * curveWidth; y: 0; controlX: curveWidth; controlY: 0}
  121. PathLine { x: shape.width - curveWidth * 2; y: 0 }
  122. PathQuad { relativeX: curveWidth; relativeY: halfHeight; relativeControlX: curveWidth; controlY: 0}
  123. PathQuad { relativeX: curveWidth; relativeY: halfHeight; relativeControlX: 0; controlY: shape.height}
  124. PathLine { x: 0; y: shape.height }
  125. }
  126. }
  127. MultiEffect {
  128. source: myshape
  129. anchors.fill: myshape
  130. blurEnabled: true
  131. blurMax: 25
  132. blur: 1.0
  133. colorizationColor: &#39;#000&#39;
  134. colorization: .2
  135. transform: Scale {
  136. xScale: 1.05
  137. origin.x: myshape.width / 2
  138. }
  139. }
  140. MyShape {
  141. id: myshape
  142. width: 200; height: 50
  143. anchors.centerIn: parent
  144. }
英文:

So, I fixed your shape, which was really a mess. Now the shape is more generic and simpler to adjust the effect scale on it. you can also change the width of curves.

To add more shadow from the sides, there is a simple solution: <br>
By using a blur effect and x-scaling it from the shape center, you can achieve the desired result.

In this example, I used MultiEffect, which is a multipurpose component in Qt6 that helps merge multiple effects into a single component.

If you are using Qt5, use FastBlur instead of drop shadow for better performance.

You can also set the colorizationColor to change the color of the shadow effect beneath the shape.

> NOTE: In my opinion, I do not advise using Shape as it is not implemented properly and has very poor results compared to other libraries like qnanopainter or pure shader.

在QML中,如何使“Shape”的“阴影”更漂亮?

  1. component MyShape: Shape {
  2. id: shape
  3. property real halfHeight: height/2
  4. property real curveWidth: height/3
  5. ShapePath {
  6. strokeWidth: 0
  7. fillColor: &#39;#fff&#39;
  8. startX: 0; startY: shape.height
  9. PathQuad { x: curveWidth; relativeY: -halfHeight; relativeControlX: curveWidth; relativeControlY: 0}
  10. PathQuad { x: 2 * curveWidth; y: 0; controlX: curveWidth; controlY: 0}
  11. PathLine { x: shape.width - curveWidth * 2; y: 0 }
  12. PathQuad { relativeX: curveWidth; relativeY: halfHeight; relativeControlX: curveWidth; controlY: 0}
  13. PathQuad { relativeX: curveWidth; relativeY: halfHeight; relativeControlX: 0; controlY: shape.height}
  14. PathLine { x: 0; y: shape.height }
  15. }
  16. }
  17. MultiEffect {
  18. source: myshape
  19. anchors.fill: myshape
  20. blurEnabled: true
  21. blurMax: 25
  22. blur: 1.0
  23. colorizationColor: &#39;#000&#39;
  24. colorization: .2
  25. transform: Scale {
  26. xScale: 1.05
  27. origin.x: myshape.width / 2
  28. }
  29. }
  30. MyShape {
  31. id: myshape
  32. width: 200; height: 50
  33. anchors.centerIn: parent
  34. }

huangapple
  • 本文由 发表于 2023年7月11日 10:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658313.html
匿名

发表评论

匿名网友

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

确定