Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved How Using Multiple ContextProperty

    General and Desktop
    qml qt5
    2
    4
    785
    Loading More Posts
    • 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.
    • MrErfan
      MrErfan last edited by

      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);
      
      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        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.

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

        1 Reply Last reply Reply Quote 0
        • MrErfan
          MrErfan last edited by

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

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion last edited by

            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
            

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post