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



  • 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!!!



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



  • 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);



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



  • 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



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



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



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



  • 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 !!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.