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
Forum Updated to NodeBB v4.3 + New Features

How Using Multiple ContextProperty

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlqt5
4 Posts 2 Posters 995 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.
  • M Offline
    M Offline
    MrErfan
    wrote on 3 Feb 2016, 06:40 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
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Feb 2016, 06:45 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
      • M Offline
        M Offline
        MrErfan
        wrote on 3 Feb 2016, 07:45 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
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 3 Feb 2016, 07:49 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

          1/4

          3 Feb 2016, 06:40

          • Login

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