Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to import an existing (singleton) c++ object to QML?
Forum Updated to NodeBB v4.3 + New Features

How to import an existing (singleton) c++ object to QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 264 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.
  • X Offline
    X Offline
    XaviP
    wrote on last edited by XaviP
    #1

    Hi Qt Forum,

    I'm trying to import an existing c++ object to QML. In main.cpp:

    Backend backend;
    QQmlApplicationEngine engine;
    engine.setInitialProperties({
        { "backend", QVariant::fromValue(&backend) }
    });
    

    In backend.h:

    class Backend : public QObject
    {
        Q_OBJECT
        QML_ELEMENT
        QML_SINGLETON
    

    It's not working:

    QQmlApplicationEngine failed to create component
    qrc:qml/main.qml: Setting initial properties failed: ApplicationWindow does not have a property called backend
    

    Do I need to implement this?:

    static Backend* create(QQmlEngine*, QJSEngine*);
    

    if engine is not going to instantiate it?

    1 Reply Last reply
    0
    • X Offline
      X Offline
      XaviP
      wrote on last edited by
      #2

      Sorry, I misunderstood some concepts. Now running with:

      Backend backend;
      QQmlApplicationEngine engine;
      engine.rootContext()->setContextProperty("backend", &backend);
      
      class Backend : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
      
      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