Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Populate C++ model from external source in Qt
Qt 6.11 is out! See what's new in the release blog

Populate C++ model from external source in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 3 Posters 4.6k Views 2 Watching
  • 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #13
    • You don't need to start from scratch with a QAbstractItemModel subclass. It's usually easier to just use QStandardItemModel and switch to the custom model only if you feel a need for performance reasons.
    • To update the model whenever you want and with whatever data you want from the C++ side you just call QAbstractItemModel::setData
    • As posted twice above, to control your model from the C++ side you need to instantiate the model in the c++ side (or have a factory on the C++ side). See the below from http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass
     AnimalModel model;
        model.addAnimal(Animal("Wolf", "Medium"));
        model.addAnimal(Animal("Polar bear", "Large"));
        model.addAnimal(Animal("Quoll", "Small"));
    
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", &model);
    

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    M 1 Reply Last reply
    2
    • VRoninV VRonin
      • You don't need to start from scratch with a QAbstractItemModel subclass. It's usually easier to just use QStandardItemModel and switch to the custom model only if you feel a need for performance reasons.
      • To update the model whenever you want and with whatever data you want from the C++ side you just call QAbstractItemModel::setData
      • As posted twice above, to control your model from the C++ side you need to instantiate the model in the c++ side (or have a factory on the C++ side). See the below from http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass
       AnimalModel model;
          model.addAnimal(Animal("Wolf", "Medium"));
          model.addAnimal(Animal("Polar bear", "Large"));
          model.addAnimal(Animal("Quoll", "Small"));
      
          QQuickView view;
          view.setResizeMode(QQuickView::SizeRootObjectToView);
          QQmlContext *ctxt = view.rootContext();
          ctxt->setContextProperty("myModel", &model);
      
      M Offline
      M Offline
      milan
      wrote on last edited by
      #14

      @VRonin . Like I have said before, calling setData function will only set the model data once or as long as it is called. I want to populate it automatically using some thread or some different method.

      VRoninV 1 Reply Last reply
      0
      • M milan

        @VRonin . Like I have said before, calling setData function will only set the model data once or as long as it is called. I want to populate it automatically using some thread or some different method.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #15

        I want to populate it automatically using some thread or some different method.

        I get the feeling that I didn't really understand your needs. To me this is trivial: emit a signal from the different thread/method and connect it to a slot that calls QAbstractItemModel::setData.

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        M 1 Reply Last reply
        0
        • VRoninV VRonin

          I want to populate it automatically using some thread or some different method.

          I get the feeling that I didn't really understand your needs. To me this is trivial: emit a signal from the different thread/method and connect it to a slot that calls QAbstractItemModel::setData.

          M Offline
          M Offline
          milan
          wrote on last edited by milan
          #16

          @VRonin. Sorry if I could not elaborate my problem enough. I am designing UI for which the backend data comes from the hardware connected to USB port. Now, I have console application that is continously requesting and sending data to/from the USB port (10 data per second). The console prints the data without problem. Next, I need to visualize those data using some GUI. I chose QT as backend data request is in C++. Looking at QT, I saw Model-view pattern is recommended for larger application. So, I tried smaller application with model, whose data-member will be updated from the data received from the USB port. I am thinking of some thread, but it is also not working. The received data has to be represented in the UI updating almost real-time if possible. I chose to update label just for testing, but it would be some different GUI in future. Please let me know my description is still not clear enough.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #17

            The description perfectly fits the above model of data update.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply
            3
            • VRoninV VRonin

              The description perfectly fits the above model of data update.

              M Offline
              M Offline
              milan
              wrote on last edited by
              #18

              @VRonin. Thanks for your reply. I am using QQmlApplicationEngine instead of QQuickView. Can I use qmlRegisterType instead of setContextProperty from QQuickView. Still, I do not understand how can calling setData will solve this problem. It has to be called again and again.

              VRoninV 1 Reply Last reply
              0
              • M milan

                @VRonin. Thanks for your reply. I am using QQmlApplicationEngine instead of QQuickView. Can I use qmlRegisterType instead of setContextProperty from QQuickView. Still, I do not understand how can calling setData will solve this problem. It has to be called again and again.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #19

                @milan said in Populate C++ model from external source in Qt:

                Can I use qmlRegisterType instead of setContextProperty from QQuickView

                No, you need the model instance to be accessible to the C++ side.

                I am using QQmlApplicationEngine instead of QQuickView.

                QQmlApplicationEngine inherits QQmlEngine so you can use applicationEngine->rootContext()->setContextProperty("myModel", &model);

                I do not understand how can calling setData will solve this problem. It has to be called again and again.

                Yes it has, something has to be called again and again to update it though, doesn't it?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                M 1 Reply Last reply
                0
                • VRoninV VRonin

                  @milan said in Populate C++ model from external source in Qt:

                  Can I use qmlRegisterType instead of setContextProperty from QQuickView

                  No, you need the model instance to be accessible to the C++ side.

                  I am using QQmlApplicationEngine instead of QQuickView.

                  QQmlApplicationEngine inherits QQmlEngine so you can use applicationEngine->rootContext()->setContextProperty("myModel", &model);

                  I do not understand how can calling setData will solve this problem. It has to be called again and again.

                  Yes it has, something has to be called again and again to update it though, doesn't it?

                  M Offline
                  M Offline
                  milan
                  wrote on last edited by
                  #20

                  @VRonin. Thank you very much for your reply. I get error when I try to add this code

                      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                      QGuiApplication app(argc, argv);
                      HardwareDataModel hardwareDataModel;
                  
                      QQmlApplicationEngine* engine;
                      engine->rootContext()->setContextProperty("HardwareDataModel", &hardwareDataModel);
                      engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
                  
                      if (engine->rootObjects().isEmpty())
                          return -1;
                  
                      return app.exec();
                  

                  When I build this, I get linker error LNK2019.

                  main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl HardwareDataModel::HardwareDataModel(class QObject *)" (??HardwareDataModel@@QEAA@PEAVQObject@@@Z) referenced in function main
                  

                  Please help.

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #21
                    1. you need to initialise engine QQmlApplicationEngine* engine = new QQmlApplicationEngine
                    2. unresolved external symbol "public: __cdecl HardwareDataModel::HardwareDataModel(class QObject *)" usually means that you have the declaration but not the implementation of that method (the constructor)

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    M 1 Reply Last reply
                    0
                    • VRoninV VRonin
                      1. you need to initialise engine QQmlApplicationEngine* engine = new QQmlApplicationEngine
                      2. unresolved external symbol "public: __cdecl HardwareDataModel::HardwareDataModel(class QObject *)" usually means that you have the declaration but not the implementation of that method (the constructor)
                      M Offline
                      M Offline
                      milan
                      wrote on last edited by milan
                      #22
                      This post is deleted!
                      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