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

Problem with inserting row to QAbstractListModel - QAbstractItemModelTester fails, but code looks alright..



  • Hi,

    I like to add a row to a custom model from the c++ side, using an own class subclassed from QAbstractListModel.

    This is the inserting code:

    void ShareDataModel::addShareData(const ShareData &shareData)
    {
        qInfo("::::::::::::: ShareDataModel::addShareData ::::::::::::::::::::\n");
        qInfo("::::::::::::: Rowcount before: %d\n", rowCount());
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        _listShareData << shareData;
        endInsertRows();
        qInfo("::::::::::::: Rowcount after: %d\n", rowCount());
    }
    

    I've attached a QAbstractItemModelTester to the model.
    When I use addShareData at program start to prefill with 2 entries, it does work fine.

    But when I call this during runtime, I get the following output in the terminal:

    Info: ::::::::::::: ShareDataModel::addShareData ::::::::::::::::::::
    Info: ::::::::::::: Rowcount before: 2
    Info: ::::::::::::: Rowcount after: 3
    Fatal: FAIL! Compared values are not the same:
       Actual (model->rowCount(parent)) 3
       Expected (c.oldSize + (end - start + 1)) 4
       (qabstractitemmodeltester.cpp:669) (qabstractitemmodeltester.cpp:822, bool __cdecl QAbstractItemModelTesterPrivate::compare<int,int>(const int &,const int &,const char *,const char *,const char *,int))
    

    As I said, 2 entries are prefilled during startup. So the debug output is looking fine.
    Rowcount is 2 before inserting the new entry and 3 after the insertion.

    I don't understand, why the tester is throwing the Fail here and is expecting 4 entries...

    As an information: this is part of an investigation why I don't see new entries listed in UI when I insert from c++ during runtime. I crawled the web and do the beginInsertRows/endInsertRows and as I'm adding one entry at the end, the rowCount(), rowCount() parameter pair for beginInsertRows should be fine as well.

    There is something I'm missing here...

    Any hints or help is appreciated,

    Best,
    Bodo



  • Hi @realBohd

    Have you maybe try create a minimum working example?

    Here is a good example how to properly create QAbstractListModel https://qmlbook.github.io/ch16-qtcpp/qtcpp.html?highlight=row



  • Thank you. I'll try a reduced example using a listview/model.
    In the meantime, maybe someone has encountered such a modeltester behaviour? I somehow suspect a threading issue here. Something like not inserting in the render thread or similar...



  • Ok, I did a simple example and ran into the same problem.
    It does only occur if I insert something to the list asynchronously from some thread.

    The weird thing is, if the thread is firing (besides the assertion from the model tester), this is coming up in the output:

    QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
    (Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)
    

    When crawling for this message, I find some hints like "Do only insert from the UI thread..."
    Is this really the way to go for an underlying 'C++' model? For my understanding, the beginInsert/endInsert should take care of this and notify the UI about changes of the underlying model..

    Sidenote: When not using the ModelTester .. the new entry is made 'interally' and if I scroll the listview it does appear as well.

    Any hints appreciated :)

    Best,
    Bodo


Log in to reply