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



  • 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?



  • 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( "," );
    }
    @



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



  • Yep. works great. Thx.

    Modified slightly to...

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


Log in to reply
 

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