Easy way to configure QML combobox with 1-25

2019-08-16 02:24发布

问题:

I have a need for a simple drop-down box containing the numbers 1 through 25 inclusive.

If I set the model up as a simple 25, I get the values 0 through 24 inclusive. I know I can have a more complicated list model but it seems rather silly to construct a massive array of values when they should be able to be calculated on the fly.

From what I read, it should be a simple matter to create a delegate for setting the display values but, with the QML:

ComboBox {
    width: 100
    model: 25
    delegate: ItemDelegate {
        text: index + 1
    }
}

the values in the drop-down selection area are correct but the value displayed on selection are still zero-based.

How do I get both the selection area and the value to be the values 1..25?

回答1:

You can set the displayText: currentIndex + 1 for the combo box:

ComboBox {
    width: 100
    model: 25
    delegate: ItemDelegate {
        text: index + 1
    }
    displayText: currentIndex + 1
}