Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Load dynmic data into a ComboBox (Musescore, QML)



  • Hi!

    I am new to QML and try to develop a plugin for the notation software Musescore.

    There, I can retrieve all staves of a score like this:

    import QtQuick 2.9
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.1
    import QtQuick.Dialogs 1.2
    import MuseScore 3.0
    
    MuseScore {
    
      // Is this a correct definition of an array property?
      var staffList
    
      function getStaves(score) {
        for(var i = 0; i < score.parts.length; i++) {
          var part = score.parts[i]
          console.log("part[i]:" + pPartName)
    
          // I would like to push the name to the list like this (but that does not seem to work)
          // staffList.push(score.parts[i])
        }
      }
    }
    

    Unfortunately I have no idea, how to save the staff names to an array of strings. The idea is to assign this string later to the comboBox's model.

    Can anybody please give me a hint, how to do that?

    Thank you!



  • @rowild Hi,
    If I understand it well you need to create a ListModel, then append the staves in it.

    https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html

    You will then straight away be able to use the model into your combobox like explained in this page.

    https://doc-snapshots.qt.io/qt5-5.11/qml-qtquick-controls-combobox.html

    Regards,

    Emmanuel


  • Qt Champions 2018

    @rowild said in Load dynmic data into a ComboBox (Musescore, QML):

    The idea is to assign this string later to the comboBox's model.

    You should directly be able to assign parts list to a ComboBox model. Use textRole to define the correct role/property to display.



  • @Allon Thank you for your answer! I knew about that approach, but I simply have no idea, where and how to put the loop over a score's staves in such a setup. The examples all have hardcoded arrays, but nowhere any for-loop (which would be the way how I think about doing it knowing a bit Javascript).
    Also the ListModel example is a separate file, if I am not mistaken. It creates 3 List Elements. How would I be able to transfer an dynamically calculated array to that file? (While writing, I am thinking of signals... "onListComplete() { MyListItemModel ... but again... there must somehow be a loop somewhere, right? ... }
    Hmmm...



  • @GrecKo Thank you very much for your input! I got something working, I write more tomorrow!



  • @rowild I think I understand what bother you. There is no loop. You just put all your data in ListModel and the internal repeater will use all of them. Once you change a ListElement in the ListModel it will update its view.
    See here
    https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html
    With a listview to understand the concept.

    Rectangle {
        width: 200; height: 200
    
        ListModel {
            id: fruitModel
            ...
        }
    
        Component {
            id: fruitDelegate
            Row {
                spacing: 10
                Text { text: name }
                Text { text: '$' + cost }
            }
        }
    
        ListView {
            anchors.fill: parent
            model: fruitModel
            delegate: fruitDelegate
        }
    }
    


  • @Allon Thank you so very much again for your help! I will have a look at it today again and report back.


Log in to reply