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] QML ListView delete element from Model
Forum Update on Monday, May 27th 2025

[Solved] QML ListView delete element from Model

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 9.2k Views
  • 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.
  • W Offline
    W Offline
    WoJo
    wrote on 12 Feb 2015, 11:28 last edited by
    #1

    I have a QML ListView, the data model is a QList<QVariant> from C++. The data inside the QVariant is a custom class. The list changes from time to time, how can I update the ListView?

    The list can be changed from the C++ code (add or remove data) or by the user from the QML Application. For example, the user can select a ListView entry and click on a delete button. Now I have the problem I don't know how to delete the selected element from QML.

    I tried to invoke a C++ method which deletes the element from the QList, this works, but the QML ListView does not recognize the change of the list.
    So I connected the destroyed() signal of the data class in the QList to a slot of the ListView delegate, here I called destroy(), but I got the message "Error: Invalid attempt to destroy() an indestructible object"

    How do I delete a object from the QML ListView?

    1 Reply Last reply
    0
    • S Online
      S Online
      sierdzio
      Moderators
      wrote on 12 Feb 2015, 11:31 last edited by
      #2

      See "this":http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html.

      You need to either reset the model after every change, or use QListItemModel.

      (Z(:^

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WoJo
        wrote on 12 Feb 2015, 12:31 last edited by
        #3

        Thanks, reset worked, but I'm not happy with this solution (could be a performance killer). QListItemModel does not exist (or am I blind?), I think you mean I have to subclass QAbstractItemModel.

        1 Reply Last reply
        0
        • S Online
          S Online
          sierdzio
          Moderators
          wrote on 12 Feb 2015, 12:40 last edited by
          #4

          Yes, if you have the time to implement a proper model, it is a better and more performant solution than the "reset" approach.

          Sorry, my bad, I've given a wrong name from memory. Here is the list model: "link":http://doc.qt.io/qt-5/qabstractlistmodel.html. It is just a minimal extension for QAbstractItemModel - you can use either of those, but since we are talking about List View, I suggested a list model :-)

          (Z(:^

          1 Reply Last reply
          0
          • W Offline
            W Offline
            WoJo
            wrote on 12 Feb 2015, 13:48 last edited by
            #5

            Thank you, I set the title to solved

            1 Reply Last reply
            0
            • S Online
              S Online
              sierdzio
              Moderators
              wrote on 12 Feb 2015, 13:53 last edited by
              #6

              Great, you are welcome, and happy coding :-)

              (Z(:^

              1 Reply Last reply
              0

              1/6

              12 Feb 2015, 11:28

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved