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

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

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

import QtQuick 2.12

Item {
    property alias text: name.text
    property alias color: btn.color
    property var old_color

    Rectangle {
        id: btn
        width: name.implicitWidth
        height: name.implicitHeight
        radius: 4
        color: "beige"
        border {
            color: Qt.darker("beige")
            width: 2
        }

        Text {
            id: name
            padding: 20
            text: "Click Me"
        }

        MouseArea {
            anchors.fill: parent

            onPressed: {
                old_color = btn.color
                btn.color = Qt.darker(btn.color)
            }

            onReleased: {
                btn.color = old_color
            }

            onClicked: {
                console.log("clicked " + name.text)
            }
        }
    }
}

// main.qml

import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480

    CustomButton {
        text: "Register"
        x: 100; y: 100
        color: "red"
    }

    CustomButton {
        text: "Login"
        x: 200; y: 200
    }

    CustomButton {
        x: 300; y: 300
    }
}

答案1

得分: 2

以下是您要翻译的内容:

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

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

import QtQuick
import QtQuick.Window

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    component CustomButton : Item {
        id: button
        property alias text: name.text
        property color color: "beige"

        Rectangle {
            id: btn
            width: name.implicitWidth
            height: name.implicitHeight
            radius: 4
            color: mouseArea.pressed ? Qt.darker(button.color) : button.color
            border {
                color: Qt.darker("beige")
                width: 2
            }

            Text {
                id: name
                padding: 20
                text: "Click Me"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
            }
        }
    }

    CustomButton {
        text: "Register"
        x: 100; y: 100
        color: "red"
    }

    CustomButton {
        text: "Login"
        x: 200; y: 200
    }

    CustomButton {
        x: 300; y: 300
    }
}
英文:

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:

import QtQuick
import QtQuick.Window

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    component CustomButton : Item {
        id: button
        property alias text: name.text
        property color color: "beige"

        Rectangle {
            id: btn
            width: name.implicitWidth
            height: name.implicitHeight
            radius: 4
            color: mouseArea.pressed ? Qt.darker(button.color) : button.color
            border {
                color: Qt.darker("beige")
                width: 2
            }

            Text {
                id: name
                padding: 20
                text: "Click Me"
            }

            MouseArea {
                id: mouseArea
                anchors.fill: parent
            }
        }
    }

    CustomButton {
        text: "Register"
        x: 100; y: 100
        color: "red"
    }

    CustomButton {
        text: "Login"
        x: 200; y: 200
    }

    CustomButton {
        x: 300; y: 300
    }
}

答案2

得分: 1

在CustomButton.qml中:

import QtQuick 2.12

Item {
    property alias text: name.text
    property alias color: btn.color
    property var old_color

    Rectangle {
        id: btn
        width: name.implicitWidth
        height: name.implicitHeight
        radius: 4
        color: "beige"
        border {
            color: Qt.darker("beige")
            width: 2
        }

        Text {
            id: name
            padding: 20
            text: "Click Me"
        }

        MouseArea {
            anchors.fill: parent

            onPressed: {    
                btn.color = Qt.darker(btn.color)
            }

            onReleased: {
                btn.color = old_color
            }

            onClicked: {
                console.log("clicked " + name.text)
            }
        }
    }
    Component.onCompleted: old_color = color.toString()
}

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

英文:

In CustomButton.qml

import QtQuick 2.12

Item {
    property alias text: name.text
    property alias color: btn.color
    property var old_color

    Rectangle {
        id: btn
        width: name.implicitWidth
        height: name.implicitHeight
        radius: 4
        color: "beige"
        border {
            color: Qt.darker("beige")
            width: 2
        }

        Text {
            id: name
            padding: 20
            text: "Click Me"
        }

        MouseArea {
            anchors.fill: parent

            onPressed: {    
                btn.color = Qt.darker(btn.color)
            }

            onReleased: {
                btn.color = old_color
            }

            onClicked: {
                console.log("clicked " + name.text)
            }

        }
    }
    Component.onCompleted: old_color = color.toString()
}

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:

确定