Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qml listview add new row to a sub vector in the model



  • I have got a custom structure defined in C++, which contains several variables among them is a QList variable. I can now add a new structure dynamically in Qml listview, but my question is I also want to add a new item into QList inside the structure, I can do this in the background but fail to update the listview.

    I think the error appears in the connect() function in C++. As I understand in order to add a new row need to call beginInsertRows() and endInsertRows(), but should be sender and receiver be the same in these two scenarios?

    connect(mList, &PropertyList::preItemAppended, this, = {
    const int index = mList->items().size();
    beginInsertRows(QModelIndex(), index, index);
    });
    connect(mList, &PropertyList::postItemAppended, this, = {
    endInsertRows();
    });

    connect(mList, &PropertyList::preParamAppended, this, [=]() {
        const int index = mList->getParams().size();
        beginInsertRows(QModelIndex(), 0, 0);
    });
    connect(mList, &PropertyList::postParamAppended, this, [=]() {
        endInsertRows();
    });

  • Lifetime Qt Champion

    Hi,

    It looks a bit convoluted. The usual implementation is rather:

    beginInsertRows(modelIndex, start, end);
    // do what needs to be done
    mList.append(item);
    endInsertRows();
    

    In the method where you trigger adding a new row.


Log in to reply