Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML reference by id. When works and when does not



  • Hello, guys
    Following the two code examples. First shows working code and Second where we write style: comboBoxStyleId shows not working code. Why when referencing by element's id we get error?

    //first
    ComboBox {
            id: combo
            anchors.centerIn: parent
            width: 200
            model: [ "Banana", "Apple", "Coconut" ]
            style: ComboBoxStyle {
                id: comboBox
                background: Rectangle {
                    id: rectCategory
    
                    radius: 5
                    border.width: 2
                    color: "#fff"
                }
            }
    
    //second
    ComboBox {
            id: combo
            anchors.centerIn: parent
            width: 200
            model: [ "Banana", "Apple", "Coconut" ]
            style: comboBoxStyleId
    
            ComboBoxStyle {
                id: comboBoxStyleId
                background: Rectangle {
                    id: rectCategory
    
                    radius: 5
                    border.width: 2
                    color: "#fff"
                }
            }
    

  • Moderators

    Hi @Kofr
    style requires a Component so you have to put ComboBoxStyle inside a Component and set its id to style.



  • @p3c0 thx, but I still do not understand why id reference to component does not work (


  • Moderators

    @Kofr Since it requires Component and thus Component's id will work.


Log in to reply