Easy way to configure QML combobox with 1-25

为君一笑 提交于 2019-12-13 03:45:35

问题


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
}


来源:https://stackoverflow.com/questions/46424298/easy-way-to-configure-qml-combobox-with-1-25

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!