Important: Please read the Qt Code of Conduct -

Component initializiation order

  • Hi,

    I have these lines of code:
    import Qt 4.7

    ListView {
    id: listview
    width: 300
    height: 500

    MongoDB {
        id: db
        name: "testdb"
        host: "localhost"
        collections: [
            MongoCollection {
                    id: mythings
                    name: "things"
    delegate: mydelegate
    model: mythings.find({})
    Component {
        id: mydelegate
        Text { text: obj.toString() }

    And my MongoDB is a QObject implemented in C++ and implementing the QDeclarativeParserStatus-interface to be able to react wait until all properties of MongoDB are set, so that I know to which host to connect.
    The problem is the line
    model: mythings.find({})
    This line is called before MongoDB's componentComplete() is called. That means that the find({}) function is called before I can determine to which host to connect.

    Can anybody tell me how to do this in the correct order?
    Or am I generally not getting the point here?



  • @Component.onCompleted: listview.model = mythings.find({})@
    Should set the model after MongoDB is set up.

    Another option might be to expose a MongoQuery object that provides the results of a search as a property (which could then notify once it had a result).

    One of the problems with using C++ functions from QML (in this case find()) is they don't always work well in bindings -- there is no way to say "call this function again, because the results have changed". We're looking at adding something like this in future versions (similar to NOTIFY for properties), at which point your original code should work as well.

    Is your QML binding of MongoDB public? I'd love to have a look.


  • Thanks, Michael.

    Yes, it's quite a nice idea. It's currently in a very early development status - as you can see, but I think I will publish a proof of concept at this weekend.
    If I've done that, I'l post it here.



  • Hi Michael,

    have a look at "this thread": or "here for plugin the code":



Log in to reply