在事件上更改QML元素的样式属性

时间:2014-03-19 12:02:02

标签: qt qml qt-quick

我有一个显示自定义组合框的组件:

    Column {
        anchors.top: parent.top
        anchors.topMargin: 200
        anchors.left: parent.left
        anchors.leftMargin: 30
        Rectangle {
            id: rectangle1
            signal comboClicked
            Column {
                id: column_button
                MenuButtonStyle {
                    id: buttonCombo
                    style: MyComboBoxButtonStyle {
                        label: ButtonTextStyle {
                            text: "Choose"
                        }
                    }
                }
            }
            Column {
                id: column1
                anchors.left: column_button.right
                anchors.leftMargin: -25
                anchors.top: parent.top
                anchors.topMargin: 3
                Image {
                    id: image1
                    width: 16
                    source: "arrow_down.png"
                }
            }                
            Keys.onReturnPressed: {
                rectangle1.state = rectangle1.state==="dropDown"?"":"dropDown"
                console.log("Open Drop Down..")
            }

            states: State {
                  name: "dropDown";
                  PropertyChanges { target: buttonCombo; style: MyButtonStyle }
            }

        }
    }

目标是更改“MenuButtonStyle”模块中的样式属性。

我用线

尝试了
PropertyChanges { target: buttonCombo; style: MyButtonStyle }

但它给了我错误:

file:///D:/projekte/qt_quick/FirstTest/MainPane.qml:82: ReferenceError: MyButtonStyle is not defined

如果我直接用“MyButtonStyle”替换“MyComboBoxButtonStyle”,QT确实抱怨未定义的引用。

有什么问题?难道不能像HTML中的CSS一样改变组件的样式吗?

1 个答案:

答案 0 :(得分:0)

我不确定您的MyButtonStyle是如何编写的,但您可以在根组件中实例化MyButtonStyle并在整个应用程序中使用它:

MyButtonStyle { id: myButtonStyle }

或者您可以将其定义为单个对象。

参考:http://qt-project.org/wiki/QmlStyling