Important: Please read the Qt Code of Conduct -

Add new item in ListView through С++ code

  • 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?

  • 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.

  • Thanks.
    QAbstractLIstModel helped

    But I still unclear why my code doesn't work

  • 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;
        return true;

    hope it helps!

Log in to reply