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. Use a c++ class as singleton inside qml files
Forum Updated to NodeBB v4.3 + New Features

Use a c++ class as singleton inside qml files

Scheduled Pinned Locked Moved QML and Qt Quick
15 Posts 4 Posters 10.5k 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.
  • T Offline
    T Offline
    thisisbhaskar
    wrote on last edited by
    #6

    ok.. one thing you can do is to connect your MyClass's onSignal some signal in your qml code.

    @QObject * root = viewer->rootObject();
    QObject *qmlItem = obj->findChild<QObject *>("qmlItem "); // "qmlItem" is objectName of one of your qml items.
    QObject::connect(&MyClass, SIGNAL(onSignal ()),qmlItem, SIGNAL(qmlItemSignal ()));@

    try it out and let us know if this works.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      billouparis
      wrote on last edited by
      #7

      Hello Vijay,

      Yes I kinda tested the "connect" thing and it worked, but it is not satisfying for me, as I may have multiple classes from which I will have to handle signals and link them to several different qml files, and I don't want to have all of them managed in my main.c file, at all. I would like everything to be handled in the QML part really.

      By the way, do you have a solution to create a singleton from within QML only?

      thank you,
      Bill

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thisisbhaskar
        wrote on last edited by
        #8

        ok :). One more thought. Have your MyClass qml instance in your root class ( I mean main.qml rootitem), and have a javascript function which finds root item, and then gets your class id.

        your javascript class something like this

        @function getSingleton(var currentItem)
        {
        var parentItem = currentItem
        while(parentItem) {
        parentItem = parentItem.parent
        }
        return parentItem.mySingletonItem
        }@

        Don't know if this works, but worth giving a try.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          billouparis
          wrote on last edited by
          #9

          Hello Vijay, thank you for your new suggestion.
          Where do I put this javascript function, and how do I declare an instance of MyClass that would be the return value of this function? I didn't understand, sorry.
          Bill

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #10

            I will try this out and let you know if this work. I should post the complete code example in case if it works.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              billouparis
              wrote on last edited by
              #11

              I'm trying to do it on my side in the mean time too, and try to make it make some sense to me ;)
              Thank you Vijay, I stay tuned!
              Bill

              1 Reply Last reply
              0
              • B Offline
                B Offline
                billouparis
                wrote on last edited by
                #12

                Got an answer from guyz @ Nokia telling me it was not a good idea in the first place, and that the singleton implementation should remain withinj the C++ code. So I think that settles it!

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thisisbhaskar
                  wrote on last edited by
                  #13

                  oho ok.. but whey is it not a good idea to have a singleton class in QML. Does it mean that we are not advised to have business logic in QML and move it to c++. And mostly we don't want to have singleton class for a displayable item and keeps moving it around??

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    billouparis
                    wrote on last edited by
                    #14

                    Here is their whole answer

                    "I have been thinking about this for a while, but didn't come up with a good solution. Besides, it feels wrong somehow trying to apply this OOP pattern to a declarative language like QML. There might be some tweaks in Javascript, but I would rather suggest to solve this in C++ or take a different approach. I mean even, if you would implement your declarative item as a singleton, the QML engine couldn't instantiate it, as it needs a public constructor."

                    Regards,
                    Bill

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Gagi
                      wrote on last edited by
                      #15

                      I know it's a old question but, did you tried to connect to the exported property using a Connections item ?

                      @
                      Connections {
                      target: objectQml
                      onSignal: //dosomething
                      }
                      @

                      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