使用QML设置自定义组件的属性不起作用,以下逻辑存在问题。

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

Setting properties of Custom Components using QML does not work, what is the problem in following logic?

问题

I have the following two qml files. 我有以下两个qml文件。
I want to change the color of the button to a darker tone when I press it and then restore the color when I release the button. 当我按下按钮时,我想将按钮的颜色改为较暗的色调,然后在释放按钮时恢复颜色。
But with every click, the button color gets darkened only, and the text on it becomes unreadable after a few clicks. 但是每次点击后,按钮的颜色只会变暗,几次点击后上面的文本变得无法阅读。
What is the actual problem with this logic? 这个逻辑的实际问题是什么?

英文:

I have following two qml files. I want to change the color of the button to darker tone when i press it and then restore the color when i release the button. But with evety click button color gets darkened only and the text on it becomes unreadable after few clicks. What is the actual problem with this logic ?

// CustomButton.qml

  1. import QtQuick 2.12
  2. Item {
  3. property alias text: name.text
  4. property alias color: btn.color
  5. property var old_color
  6. Rectangle {
  7. id: btn
  8. width: name.implicitWidth
  9. height: name.implicitHeight
  10. radius: 4
  11. color: "beige"
  12. border {
  13. color: Qt.darker("beige")
  14. width: 2
  15. }
  16. Text {
  17. id: name
  18. padding: 20
  19. text: "Click Me"
  20. }
  21. MouseArea {
  22. anchors.fill: parent
  23. onPressed: {
  24. old_color = btn.color
  25. btn.color = Qt.darker(btn.color)
  26. }
  27. onReleased: {
  28. btn.color = old_color
  29. }
  30. onClicked: {
  31. console.log("clicked " + name.text)
  32. }
  33. }
  34. }
  35. }

// main.qml

  1. import QtQuick.Window 2.12
  2. Window {
  3. visible: true
  4. width: 640
  5. height: 480
  6. CustomButton {
  7. text: "Register"
  8. x: 100; y: 100
  9. color: "red"
  10. }
  11. CustomButton {
  12. text: "Login"
  13. x: 200; y: 200
  14. }
  15. CustomButton {
  16. x: 300; y: 300
  17. }
  18. }

答案1

得分: 2

以下是您要翻译的内容:

根据folibis在您的问题评论中所说,您在颜色属性上使用var类型的方式会创建一个值类型引用。这就像是引用/指针,因此颜色对象会传递并被重复使用,导致颜色变得越来越暗。要解决这个问题,请将您的属性类型设置为color。一般来说,如果可能的话,总是使用QML类型,而不是依赖于var

也可以不需要像这样的赋值来编写您的按钮:

  1. import QtQuick
  2. import QtQuick.Window
  3. Window {
  4. width: 640
  5. height: 480
  6. visible: true
  7. title: qsTr("Hello World")
  8. component CustomButton : Item {
  9. id: button
  10. property alias text: name.text
  11. property color color: "beige"
  12. Rectangle {
  13. id: btn
  14. width: name.implicitWidth
  15. height: name.implicitHeight
  16. radius: 4
  17. color: mouseArea.pressed ? Qt.darker(button.color) : button.color
  18. border {
  19. color: Qt.darker("beige")
  20. width: 2
  21. }
  22. Text {
  23. id: name
  24. padding: 20
  25. text: "Click Me"
  26. }
  27. MouseArea {
  28. id: mouseArea
  29. anchors.fill: parent
  30. }
  31. }
  32. }
  33. CustomButton {
  34. text: "Register"
  35. x: 100; y: 100
  36. color: "red"
  37. }
  38. CustomButton {
  39. text: "Login"
  40. x: 200; y: 200
  41. }
  42. CustomButton {
  43. x: 300; y: 300
  44. }
  45. }
英文:

As folibis said in the comment on your question the way you are using the var type for the color property will create a Value Type Reference. This acts like a reference/pointer hence the color object is passed around and re-used resulting in the color becoming darker and darker. To solve that use color as the type of your property. As a rule of thumb always use a QML type if possible instead of relying on var.

That said you can also write your button without the need of assignments like this:

  1. import QtQuick
  2. import QtQuick.Window
  3. Window {
  4. width: 640
  5. height: 480
  6. visible: true
  7. title: qsTr("Hello World")
  8. component CustomButton : Item {
  9. id: button
  10. property alias text: name.text
  11. property color color: "beige"
  12. Rectangle {
  13. id: btn
  14. width: name.implicitWidth
  15. height: name.implicitHeight
  16. radius: 4
  17. color: mouseArea.pressed ? Qt.darker(button.color) : button.color
  18. border {
  19. color: Qt.darker("beige")
  20. width: 2
  21. }
  22. Text {
  23. id: name
  24. padding: 20
  25. text: "Click Me"
  26. }
  27. MouseArea {
  28. id: mouseArea
  29. anchors.fill: parent
  30. }
  31. }
  32. }
  33. CustomButton {
  34. text: "Register"
  35. x: 100; y: 100
  36. color: "red"
  37. }
  38. CustomButton {
  39. text: "Login"
  40. x: 200; y: 200
  41. }
  42. CustomButton {
  43. x: 300; y: 300
  44. }
  45. }

答案2

得分: 1

在CustomButton.qml中:

  1. import QtQuick 2.12
  2. Item {
  3. property alias text: name.text
  4. property alias color: btn.color
  5. property var old_color
  6. Rectangle {
  7. id: btn
  8. width: name.implicitWidth
  9. height: name.implicitHeight
  10. radius: 4
  11. color: "beige"
  12. border {
  13. color: Qt.darker("beige")
  14. width: 2
  15. }
  16. Text {
  17. id: name
  18. padding: 20
  19. text: "Click Me"
  20. }
  21. MouseArea {
  22. anchors.fill: parent
  23. onPressed: {
  24. btn.color = Qt.darker(btn.color)
  25. }
  26. onReleased: {
  27. btn.color = old_color
  28. }
  29. onClicked: {
  30. console.log("clicked " + name.text)
  31. }
  32. }
  33. }
  34. Component.onCompleted: old_color = color.toString()
  35. }

old_color应在Item创建后保持其初始值。

英文:

In CustomButton.qml

  1. import QtQuick 2.12
  2. Item {
  3. property alias text: name.text
  4. property alias color: btn.color
  5. property var old_color
  6. Rectangle {
  7. id: btn
  8. width: name.implicitWidth
  9. height: name.implicitHeight
  10. radius: 4
  11. color: "beige"
  12. border {
  13. color: Qt.darker("beige")
  14. width: 2
  15. }
  16. Text {
  17. id: name
  18. padding: 20
  19. text: "Click Me"
  20. }
  21. MouseArea {
  22. anchors.fill: parent
  23. onPressed: {
  24. btn.color = Qt.darker(btn.color)
  25. }
  26. onReleased: {
  27. btn.color = old_color
  28. }
  29. onClicked: {
  30. console.log("clicked " + name.text)
  31. }
  32. }
  33. }
  34. Component.onCompleted: old_color = color.toString()
  35. }

old_color should keep its initial value after Item is created.

huangapple
  • 本文由 发表于 2023年6月8日 13:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428945.html
匿名

发表评论

匿名网友

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

确定