Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    QML Combobox with QStringListModel

    QML and Qt Quick
    3
    6
    3582
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      Valerian last edited by

      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.

      raven-worx 1 Reply Last reply Reply Quote 1
      • raven-worx
        raven-worx Moderators @Valerian last edited by

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        V 1 Reply Last reply Reply Quote 0
        • V
          Valerian @raven-worx last edited by Valerian

          @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

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @Valerian last edited by raven-worx

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

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • jpnurmi
              jpnurmi last edited by

              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
              }
              
              V 1 Reply Last reply Reply Quote 1
              • V
                Valerian @jpnurmi last edited by

                @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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post