Important: Please read the 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, = {

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

  • Lifetime Qt Champion


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

    beginInsertRows(modelIndex, start, end);
    // do what needs to be done

    In the method where you trigger adding a new row.

Log in to reply