How Using Multiple ContextProperty



  • hi guys ,
    I want to use two Context Property in main.cpp , When I run the program stopped program ,But when the one I use Context Property Program runs correctly
    Error : .......Desktop_Qt_5_5_1_MSVC2013_64bit-Debug\debug\Application.exe exited with code 255
    my code :

        SqlQueryModel *sqlquerymodel = new SqlQueryModel();
        sqlquerymodel->connecttodb();
        sqlquerymodel->setQuery("SELECT * FROM tbl_user");
        engine.rootContext()->setContextProperty("SqlQueryModel", sqlquerymodel);
    
        MyClass myclass;
        engine.rootContext()->setContextProperty("MyClass",&myclass);
    

  • Moderators

    Where is this code located?

    MyClass myclass;
    engine.rootContext()->setContextProperty("MyClass",&myclass);
    

    If it is in a method then myclass is destroyed when the method finishes and your context contains a pointer to something which is already destroyed. Make sure myclass exists during the time the pointer to it is stored in the context.



  • thanks for your response
    That is, only once can use Context Property ? So I turned to my two classes into one class


  • Moderators

    No, that's not what I said.
    The problem was that you stored a pointer to something what was destroyed. As soon as you will try to access it your application will crash.

    void SomeClass::someMethod()
    {
        MyClass myclass;
        engine.rootContext()->setContextProperty("MyClass",&myclass); <-- here you store pointer to myclass
    } <-- here myclass is destroyd, so the pointer you stored in engine is not valid anymore
    

Log in to reply
 

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