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. Return qml component from c++ function
Forum Updated to NodeBB v4.3 + New Features

Return qml component from c++ function

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 5 Posters 399 Views 2 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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by
    #1

    Hello,

    I have a C++ class which acts as a factory, creating two different classes which implement the same interface.

    I already registered that factory as a singleton via QML_ELEMENT QML_SINGLETON which works perfectly. Now I want to call my create function , which returns the correct implementation, from qml.

    Is this possible? If so, how?

    B GrecKoG MesrineM 3 Replies Last reply
    0
    • R Redman

      Hello,

      I have a C++ class which acts as a factory, creating two different classes which implement the same interface.

      I already registered that factory as a singleton via QML_ELEMENT QML_SINGLETON which works perfectly. Now I want to call my create function , which returns the correct implementation, from qml.

      Is this possible? If so, how?

      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @Redman these C++ classes that implement the same interface - how do you want to use them in your QML? Where would the factory fit in on the QML side?

      1 Reply Last reply
      0
      • R Redman

        Hello,

        I have a C++ class which acts as a factory, creating two different classes which implement the same interface.

        I already registered that factory as a singleton via QML_ELEMENT QML_SINGLETON which works perfectly. Now I want to call my create function , which returns the correct implementation, from qml.

        Is this possible? If so, how?

        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        @Redman make the create function Q_INVOKABLE : Exposing Methods (Including Qt Slots)

        Axel SpoerlA 1 Reply Last reply
        0
        • GrecKoG GrecKo

          @Redman make the create function Q_INVOKABLE : Exposing Methods (Including Qt Slots)

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          @GrecKo
          So create() is supposed to return instances of other C++ classes, that are exposed to QML, just not as singletons?

          I assume that you are familiar with this, since you have already succeeded with your singleton.

          You need to expose the other classes just like the factory. As @GrecKo said, you implement a

          Q_INVOKABLE NewClass *Factory::create(MaybeAnEnum type)
          {
              switch (type) {
              case Redman: return new NewClassRedMan;
              case GrecKo: return new NewClassGrecKo;
              case Axel: return new NewClassAxel;
              }
          }
          

          To use it in QML, define a property of the appropriate type and assign the return value of create().

          Rectangle {
              id: window
              property QtObject myClass: mySingleton.create(Redman)
          }
          

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          0
          • R Redman

            Hello,

            I have a C++ class which acts as a factory, creating two different classes which implement the same interface.

            I already registered that factory as a singleton via QML_ELEMENT QML_SINGLETON which works perfectly. Now I want to call my create function , which returns the correct implementation, from qml.

            Is this possible? If so, how?

            MesrineM Offline
            MesrineM Offline
            Mesrine
            wrote on last edited by Mesrine
            #5

            @Redman said in Return qml component from c++ function:

            Are you referring to
            T*create(QQmlEngine *, QJSEngine *) function
            from this docs ?

            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