Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. use c++ Listmodel object in qml
Forum Updated to NodeBB v4.3 + New Features

use c++ Listmodel object in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 866 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hassonH Offline
    hassonH Offline
    hasson
    wrote on last edited by hasson
    #1

    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
    }
    }

    jsulmJ 2 Replies Last reply
    0
    • hassonH Offline
      hassonH Offline
      hasson
      wrote on last edited by
      #9

      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~

      1 Reply Last reply
      0
      • hassonH hasson

        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
        }
        }

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • hassonH Offline
          hassonH Offline
          hasson
          wrote on last edited by hasson
          #3

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

          1 Reply Last reply
          0
          • hassonH hasson

            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
            }
            }

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • hassonH Offline
              hassonH Offline
              hasson
              wrote on last edited by
              #5

              @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

              IntruderExcluderI 1 Reply Last reply
              0
              • hassonH hasson

                @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

                IntruderExcluderI Offline
                IntruderExcluderI Offline
                IntruderExcluder
                wrote on last edited by
                #6

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

                GrecKoG 1 Reply Last reply
                2
                • hassonH Offline
                  hassonH Offline
                  hasson
                  wrote on last edited by
                  #7

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

                  1 Reply Last reply
                  0
                  • IntruderExcluderI IntruderExcluder

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

                    GrecKoG Offline
                    GrecKoG Offline
                    GrecKo
                    Qt Champions 2018
                    wrote on last edited by
                    #8

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

                    1 Reply Last reply
                    0
                    • hassonH Offline
                      hassonH Offline
                      hasson
                      wrote on last edited by
                      #9

                      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~

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved