QML Combobox with QStringListModel



  • Hi all,

    I have a QStringListModel which is configured using the setStringList method.

    I'm trying to use this model in QML Combobox.

    I can't find a way to do so.

    Please suggest.


  • Moderators

    @Valerian
    QStringListModel::setStringList() is neither a slot nor declared invokable, thus you have no chance to call it from QML.
    You would need to subclass QStringListModel class and add a callable method yourself. Don't forget to register this custom list model type.



  • @raven-worx But QStringListModel inherits QAbstractItemModel.

    And I have exposed this model to qml using setContextProperty but I can't find the role name to be used in QML


  • Moderators

    @Valerian
    k, i thought you want to call the setStringList() method from within QML.
    Try to register QStringListModel class - using qmlRegisterUncreatableType() is sufficient



  • The missing piece is probably to specify "display" as a text role. Any QAbstractItemModel derivative model has a bunch of default roles. ComboBox doesn't know which one to show:

    ComboBox {
        textRole: "display" // <==
        model: stringListModel
        anchors.centerIn: parent
    }


  • @jpnurmi Hi,

    Thanks for the reply.

    Is there any way to disable selection in the combobox? I want to use the combobox to show the first item and not allow click on the item at index 1 or index 2 .

    Kindly advice


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.