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. [Solved] Method or Function to load a ComboBox model dynamically ?
Forum Updated to NodeBB v4.3 + New Features

[Solved] Method or Function to load a ComboBox model dynamically ?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    PSI-lbc
    wrote on last edited by
    #1

    You can do this inline by:

    @ ComboBox {
    model: [ "item1,item2,item3" ]
    }@

    But I'd like to be able to do something more like:

    @ VisualItemModel {
    id: itemModel

    Thing { myComboModelString: "item1,item2,item3" }
    }

    ListView {
    anchors.fill: parent
    model: itemModel
    }


    Thing.qml

    Rectangle {
    id: thing

    property string myComboModelString

    ComboBox {
    model: myComboModelString
    }
    }
    @

    So is there a method or function I'm missing that will take a delimited string and load a model? Or do I have to create a function in either QML/C++ myself to accomplish this?

    1 Reply Last reply
    0
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

      I'm quite sure to got what you want to do.
      That's because, your first snippet code it's wrong. You cannot do:
      @
      ComboBox {
      model: [ "item1,item2,item3" ]
      }
      @
      But you should do the following:
      @
      ComboBox {
      model: [ "item1", "item2", "item3" ]
      }
      @

      And from what I know there no functions for splitting a delimited string into a list from QML side. But you can do on C++ using QString::split method, and from C++ you can return directly the QStringList to the model property of ComboBox:

      @
      ComboBox {
      model: cppObject.splitString( myComboModelString )
      }
      @

      And from C++:
      @
      QStringList CppObject::splitString( QString myComboModelString ) {
      return myComboModelString.split( "," );
      }
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PSI-lbc
        wrote on last edited by
        #3

        Yep. the 1st snippet was a typo..should have just cut&pasted instead of retyping.

        Scoured the docs for something with no luck..so it's off to cpp code. Thanks for the cpp snippet. Should be easy to integrate. Will test and post back.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PSI-lbc
          wrote on last edited by
          #4

          Yep. works great. Thx.

          Modified slightly to...

          @QStringList CppObject::splitString( QString sepChar, QString myString ) {
          return myString.split( sepChar );
          }
          @

          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