Question

In QML I have a QComboBox:

ComboBox {
    id: myCBox
    model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]
 }

When I try to find an element in the ComboBox by text it returns -1.

Log with this:

console.log(myCBox.find("5"))

And it outputs -1 (which means not found).

QML QComboBox documentation

Was it helpful?

Solution

You should check, when do you the call myCBox.find, look at this code:

ComboBox {
    id: myCBox
    model: [ "1.5", "2", "2.5", "3", "3.5", "4", "5", "6", "7", "8", "9" ]

    Component.onCompleted: {
        console.log("After this line it should work fine.");
    }

    Item {
        Component.onCompleted: {
            console.log("myCBox is not completed:", myCBox.find("5"));
        }
    }
}

Component.onCompleted: {
    console.log("myCBox here must be completed:", myCBox.find("5"));
}

and the output:

myCBox is not completed: -1
After this line it should work fine.
myCBox must be completed: 6

Uppon creation, Component creates all items and then arranging them in a tree. From the most inner object, it updates properties and bindings to the most overrided value and calls attached method Component.onCompleted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top