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. Two QML classes connected to the same C++ class!
Forum Updated to NodeBB v4.3 + New Features

Two QML classes connected to the same C++ class!

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 733 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    skammers
    wrote on last edited by
    #1

    Hey!

    I have to different QML classes, which I want to connect to the same c++ class. I tried importing the C++ class in both QML classes, but then I get two different instances /objects of the c++ class. I wonder if there is a way to connect both of them to the same object?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Create C++ object and export to QML using setContextProperty. Use this object in QML. Exporting the classes will export class and not the object.

      e.g
      @UserData *user = new UserData("pthinks.com","pthinks123");

      //Get the QML Engine
      QQmlEngine *eng = view.engine();
      QQmlContext *context = eng->rootContext();
      context->setContextProperty("UserDB",user);
      

      @

      UserDB is accessible in QML files. Hope this is what you are asking for.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      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