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. How Using Multiple ContextProperty

How Using Multiple ContextProperty

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlqt5
4 Posts 2 Posters 1.0k 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.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by
    #1

    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
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • MrErfanM Offline
        MrErfanM Offline
        MrErfan
        wrote on last edited by
        #3

        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
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved