Qt 文本字段更改背景

Qt 文本字段更改背景,qt,qml,textfield,Qt,Qml,Textfield,我有一个自定义按钮,当按下该按钮时,我想将没有写入文本的文本字段的颜色从默认值更改为红色2秒钟,并弹出一个工具提示,说明在继续之前必须在其中写入内容 TextField { id: textfield_derivat placeholderText: qsTr("Write a comment here...") horizontalAlignment: Text.AlignHCenter } CustomSelectionButton { id: go_bu

我有一个自定义按钮,当按下该按钮时,我想将没有写入文本的文本字段的颜色从默认值更改为红色2秒钟,并弹出一个工具提示,说明在继续之前必须在其中写入内容

TextField {
    id: textfield_derivat
    placeholderText: qsTr("Write a comment here...")
    horizontalAlignment: Text.AlignHCenter
}


CustomSelectionButton {
    id: go_button
    x: 378
    y: 342
    text: "Let's go!"
    anchors.rightMargin: 19
    anchors.bottomMargin: 17
    anchors.bottom: parent.bottom
    anchors.right: parent.right
    height: default_button_height - 10
    font.pointSize: default_font_size
    MouseArea {
        anchors.fill: parent
        onClicked: {
            if( check_text_field(textfield_derivat.text) === true )
            {
                /* code here */
            }
            else
            {
                textfield_derivat.text = "Please fill up the text"
                textfield_derivat.background = "red"
            }

这是因为
background
QQuickItem
而不是QString。你应该这样写(确保你的初始颜色是你喜欢的):

摘自:

请注意,QML中有
动画
,以及使用
状态
转换
,这可能会使解决方案更易于阅读

请提供
TextField {
    id: textfield_derivat
    placeholderText: qsTr("Write a comment here...")
    horizontalAlignment: Text.AlignHCenter

    background: Rectangle { 
        id: txt_back
        implicitWidth: 200
        implicitHeight: 40
        color: "white" 
    }
}

...

//textfield_derivat.background = "red"
txt_back.color = "red"