Easy way to configure QML combobox with 1-25

2019-08-16 02:53发布

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条回答
【Aperson】
2楼-- · 2019-08-16 03:06

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

ComboBox {
    width: 100
    model: 25
    delegate: ItemDelegate {
        text: index + 1
    }
    displayText: currentIndex + 1
}
查看更多
登录 后发表回答