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. Add new item in ListView through С++ code
Forum Updated to NodeBB v4.3 + New Features

Add new item in ListView through С++ code

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.8k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    Trikrista
    wrote on last edited by
    #1

    I want to add now Item in ListView through С++ code, with a check for originality.

    I do it like this:
    But code do not add new item, and change old item. (it see in debugger).
    What have I done wrong?

    I'm sorry for my bad English

    1 Reply Last reply
    0
    • MonomixM Offline
      MonomixM Offline
      Monomix
      wrote on last edited by Monomix
      #2

      A couple of things:

      • You should probably derive your DataList class from QAbstractListModel rather than QObject - it's made to do what you want here
      • When adding items to your DataList, make sure to call beginInsertRows before and endInsertRows after.
      1 Reply Last reply
      1
      • T Offline
        T Offline
        Trikrista
        wrote on last edited by
        #3

        Thanks.
        QAbstractLIstModel helped

        But I still unclear why my code doesn't work

        I'm sorry for my bad English

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          I used this in my code:

          bool ImageModel::addImage(QString id, QString description, int duration_secs)
          {
          
              beginInsertRows(QModelIndex(), m_imageList.count(), m_imageList.count());
              StructImage struct_image;
              m_imageList << struct_image;
              endInsertRows();
              return true;
          }
          

          hope it helps!

          1 Reply Last reply
          0

          • Login

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