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. How to allow data to be exposed to the QML components using QQmlContext
Forum Updated to NodeBB v4.3 + New Features

How to allow data to be exposed to the QML components using QQmlContext

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 2 Posters 2.4k 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.
  • J Offline
    J Offline
    jiji
    wrote on last edited by
    #1

    Hi,
    I can't expose my data to the QML components instantiated by the QML engine when I declare QQmlcontext outside the main method.
    Can you help me please!!!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Can you show the sample code ? This helps us to help you.

      Some info for you. when engine should have context automatically. Y r u creating the context separately ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jiji
        wrote on last edited by
        #3

        int main(int argc, char argv[])
        {
        QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        qputenv("QT_QUICK_CONTROLS_STYLE", "material");
        QGuiApplication app(argc, argv);
        ApplicationUI appui;
        QQmlApplicationEngine engine;
        QQmlContext
        context = engine.rootContext();
        context->setContextProperty("myApp", &appui);

        //liste des troc
        QList<Troc*> listtroc;
        listtroc=T.getAllTroc();
        EntityModel model;
        for(int i =0;i<listtroc.size();i++ )
        {

           QString title = listtroc[i]->title();
           QString body = listtroc[i]->body();
           model.addEntity(Entity(title,body));
        

        context->setContextProperty("myModel", &model);
        engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
        return app.exec();
        }

        when I put this code in another method : myModel become undefined on QML components

        QList<Troc*> listtroc;
        listtroc=T.getAllTroc();
        EntityModel model;
        for(int i =0;i<listtroc.size();i++ )
        {

           QString title = listtroc[i]->title();
           QString body = listtroc[i]->body();
           model.addEntity(Entity(title,body));
        

        context->setContextProperty("myModel", &model);

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by dheerendra
          #4

          It is because your entitymodel object is local variable and gets destroyed when function call is over. Main never exits because of exec function call. So because of this your object still exist. Create object in heap EntityModel *mymodel = new EntityModel. It should work.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          2
          • J Offline
            J Offline
            jiji
            wrote on last edited by
            #5

            Hi,
            @dheerendra I changed my code lik this:
            QQmlApplicationEngine engine;
            QQmlContext* context = engine.rootContext();
            Troc troc;

                          QList<Troc*> listtroc;
                          listtroc=troc.getallTroc();
                             qDebug()<<"troc list"<< listtroc;
                             EntityModel *mymodel = new EntityModel;
                            for(int i =0;i<listtroc.size();i++ )
                             {
            

            QString title = listtroc[i]->title();
            QString body = listtroc[i]->body();
            mymodel->addEntity(Entity(title,body));
            }
            context->setContextProperty("myModel", QVariant::fromValue(mymodel));
            But nothing has changed always I got this error:
            qrc:/qml/pages/HomePage.qml:323: ReferenceError: myModel is not defined

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              Looks like your are doing some programming mess-up.

              Here is sample program which I have prepared and works. You can check and see where is the problem you are facing.

              ==main.cpp===

              void registerObject(QQmlContext *context) {
              MyModel *model = new MyModel;
              context->setContextProperty("myModel",model);
              }
              // Above function registers the object to qmlcontext
              int main(int argc, char *argv[])
              {
              QGuiApplication app(argc, argv);
              QQmlApplicationEngine engine;
              registerObject(engine.rootContext());
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              return app.exec();
              }

              === MyModel.cpp===
              #include "mymodel.h"

              MyModel::MyModel(QObject *parent) : QObject(parent){}

              void MyModel::callMe(){ qDebug() << Q_FUNC_INFO << this->objectName() << ends;}

              == main.qml====
              Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")

              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      myModel.callMe();
                  }
              }
              

              }

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              2
              • J Offline
                J Offline
                jiji
                wrote on last edited by
                #7

                @dheerendra I have a problem when I need to refresh my list, not the simple entity.
                I get my list on QML interface, but when I add a new entity the model doesn't refresh, because I got (My model (my list) from the main).
                So I want to reload my new list on the QML interface when I add the new entity but it's doesn't work.

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  If you want to refresh the model, exposing the list like the way you done is not answer Customise the model in C++ and implement appropriate virtual functions. Refresh should happen automatically. You don't have to do anything. Look at objectList example in Qt Example. It is good one.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    jiji
                    wrote on last edited by
                    #9

                    But I'm taking my list as Json list from the server than transfer it to the modellist.

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jiji
                      wrote on last edited by
                      #10

                      Hi @dheerendra ,
                      My listView get updated when I run the application one more time, but I want refresh it when I add a new entity in my data base without close and run the application every time.
                      I hope you can help me !!!

                      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