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

use c++ Listmodel object in qml



  • recently,I wrote a class inherites from QAbstractListModel called listModel, I wanna use this model in qml, and I have registered it in main.c by setContextProperty function. The first running it worked well, but when listModel's data changed, the app showed a "segment fault".
    in main.c
    QQmlApplicationEngine engine;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/Main.qml")));
    QObject* object = component.create();
    app.setObjectInstance(object);

    engine.rootContext()->setContextProperty("screenModel",
                                     &ScreenListModel::getInstance());
    

    ...
    in my qml file
    GridView {
    anchors.centerIn: parent
    width: 300
    height: 70
    model: screenModel
    delegate: UserButton {
    nameCn: model.name_cn
    nameEn: model.name_en
    }
    }



  • I guess I have found the issue, because the function where operate QAbstractListModel is in a normal thread , when used a signal/slot ,the app works well.
    thank you all~


  • Lifetime Qt Champion

    @hasson Please show your code, else others can only guess...



  • code is update , could you help me pls, when GridView is not defined ,it works well


  • Lifetime Qt Champion

    @hasson said in use c++ Listmodel object in qml:

    The first running it worked well, but when listModel's data changed, the app showed a "segment fault".

    Did you run your app through debugger?
    Do so and take a look at the stack trace (you can post it here, so others can check).
    How do you change listModel data?
    What do you do in ScreenListModel?



  • @jsulm said in use c++ Listmodel object in qml:

    What do you do in ScreenListModel?
    thanks,
    I insert model item by this way:
    QList<ScreenInfo> m_list;
    void ScreenListModel::insert(int index, const ScreenInfo &si)
    {
    if(index < 0 || index > m_list.count()) {
    return;
    }
    emit beginInsertRows(QModelIndex(), index, index);
    m_list.insert(index, si);
    emit endInsertRows();
    }

    the app is running on arm board so i can't see the stack trace,but the problem is occured while inserting a item,the operation is on a thread ,ScreenListModel is a singleton



  • beginInsertRows and endInsertRows are not signals, they should not be emitted.



  • thanks, i found it got crashed when i inserted item in another thread, but it works well in main thread


  • Qt Champions 2018

    @IntruderExcluder said in use c++ Listmodel object in qml:

    beginInsertRows and endInsertRows are not signals, they should not be emitted.

    While true, this won't change anything since emit just expands to nothing. emit x = x.
    It's better for others reading your code though.



  • I guess I have found the issue, because the function where operate QAbstractListModel is in a normal thread , when used a signal/slot ,the app works well.
    thank you all~


Log in to reply