Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Combobox with QStringListModel

QML Combobox with QStringListModel

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 3 Posters 4.1k Views
  • 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 Offline
    V Offline
    Valerian
    wrote on last edited by
    #1

    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-worxR 1 Reply Last reply
    1
    • V Valerian

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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
      0
      • raven-worxR raven-worx

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

        V Offline
        V Offline
        Valerian
        wrote on last edited by Valerian
        #3

        @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-worxR 1 Reply Last reply
        0
        • V 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-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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
          0
          • jpnurmiJ Offline
            jpnurmiJ Offline
            jpnurmi
            wrote on last edited by
            #5

            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
            1
            • jpnurmiJ jpnurmi

              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 Offline
              V Offline
              Valerian
              wrote on last edited by
              #6

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

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved