Please nominate your Qt Champions for 2021!

[SOLVED] QQmlListProperty and append, at, clear, count - how to use them?

  • So "here": you can see QQmlListProperty. I set everything up (list of QObjects, accessible from QML) and have no idea how to call the methods "clear" etc. (yes, I handed over the pointers) from within QML. I would highly appreciate some clear instructions.

    Also a very frustrated note on QML - I suggest not reading it^^: "Designing" my first window with about 2.5 buttons in it took as long as writing the approximately 2000 lines of C++ code backing up that window - the documentation is full of errors, in most cases way too brief and incomplete (I have been spoiled by Qts documentation, I admit), the language seems completely non-intuitive to me since I have to constantly look up ever-changing properties which then are described in less than ten words, without existing (or linked) examples nearby (and if there are any, they are faulty)...
    I was really looking forward to QML, but after this little project is done, I will probably go back to Qt and wait for its deformed step-step-stepbrother to mature a little bit.
    (I just spend several hours trying to call the "clean()"-method on my QObject-list in QML, I apologize for all the brain-damage I must have taken.)

    PS: Thanks in advance for any kind of help. :)

  • Have you tried setting your list property equal to @[]@ (an empty javascript array). The idea behind QQmlListProperty is enable it to have an interface closely resembling that of a javascript array.

    Hang in there, there is a steep learning curve, but once you've gotten through it you won't want to go back to QWidget :).

  • Well, that does something - thanks so far :) . But now the program crashes when I "empty" my list (I have to assume that's what happens, even though I can't see it before the crash).

    I am displaying the lists items in a ListView - it seems that doesn't get updated when I clear the list. Do you maybe have another hint to give me? I am pretty sure there is no ListView.clear(), I would have found that during my struggles yesterday. ;)

    EDIT: I now know how to prevent the crash, but again the solution seems super-non-intuitive:
    Instead of calling "myobject.mycustomQmlListProperty = []", which crashes the program, I have to call "ListView.model = []", even though I also set "ListView.model = myobject.mycustomQmlListProperty" not long before. This kind of fragile behavior is what puts me off about QML.

    Thanks again for the help, I will mark this as solved (and probably open up more threads soon :( ).

    Just to answer more of the initial question: is accessed via model[i] (makes sense)
    model.count() can be accessed via model.length
    model.append() ... well, I have no idea - a couple more weeks with QML and I might know!

  • I am not a big fan of QQmlListProperty myself. I prefer to make QAbstractItemModels when I need models for my list views. Only in very specific cases will I resort to QQmlListProperty.

  • I read about that, and that seems to be a completely different approach. Had I read about it before having spent fifty hours with QQmlListProperty, I probably would have chosen that route instead (especially since the coding would have been familiar to me).
    However, by now I am too deep into QQmlListProperty to just quit.

    Do you know of any minimal (while fully functional) examples of how to use a QAbstractItemModel from QML? Or maybe you happen to at least know some buzzwords I can feed into google?

  • I've seen the pages but skipped over the "model"-part - thanks for the link, I'll have a look. Maybe I will need to know how to circumvent using QQmlListProperty in the future. ;)

Log in to reply