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. Singleton signal c++ qml example

Singleton signal c++ qml example

Scheduled Pinned Locked Moved QML and Qt Quick
15 Posts 3 Posters 7.7k 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.
  • X Offline
    X Offline
    xenotrax
    wrote on last edited by
    #5

    I use the example code from Qt.. but I wrote myApi instead of MyApi
    but there is still a red line underneath the
    @
    import Qt.example.qobjectSingleton 1.0
    @
    statement

    Usage:
    // first, define your QObject which provides the functionality.
    @
    class SingletonTypeExample : public QObject
    {
    Q_OBJECT
    Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)

    public:
    SingletonTypeExample(QObject* parent = 0)
    : QObject(parent), m_someProperty(0)
    {
    }

    ~SingletonTypeExample() {}
    
    Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; }
    
    int someProperty() const { return m_someProperty; }
    void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
    

    signals:
    void somePropertyChanged(int newValue);

    private:
    int m_someProperty;
    };

    // second, define the singleton type provider function (callback).
    static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)

    SingletonTypeExample *example = new SingletonTypeExample();
    return example;
    

    }
    @
    // third, register the singleton type provider with QML by calling this function in an initialization function.
    @
    ...
    qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
    ...
    @
    In order to use the registered singleton type in QML, you must import the singleton type.
    @
    import QtQuick 2.0
    import Qt.example.qobjectSingleton 1.0
    Item {
    id: root
    property int someValue: MyApi.someProperty

    Component.onCompleted: {
        someValue = MyApi.doSomething()
    }
    

    }
    @
    Since singleton types do not have an associated QQmlContext object, then within the functions of a QObject-derived type that is registered as a singleton type implementation the QML context and engine information is not available. The QQmlEngine::contextForObject() function returns NULL when supplied with a pointer to an QObject that implements a singleton type.

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xenotrax
      wrote on last edited by
      #6

      @
      #include <QtQml>
      #include "singletontypeexample.h"

      int main(int argc, char* argv[])
      {
      ...
      qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
      ...

      @

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #7

        Instead of
        @
        import Qt.example.qobjectSingleton 1.0
        @

        try
        @
        import Qt.example.qobjectSingleton 1.0 as ExampleApi
        property int someValue: ExampleApi.MyApi.someProperty
        @

        From the docs.

        157

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xenotrax
          wrote on last edited by
          #8

          Yes I can access the value .. "ExampleApi.MyApi.someProperty" there is no error message
          But still a red line underneath the Import statement and no code completion

          is this maybe normal on Qt if I use pointers ?

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #9

            This could be the parsing problems in QtCreator. Try restarting the QtCreator.

            157

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lselle
              wrote on last edited by
              #10

              Hello.

              I have exact the same problem.

              At this time i have create a QtQuick Plugin project and register my singletontype in this project. In my other project i have add QML_IMPORT_PATH = PathToMyQtQuickPlugin and now i have no red line underneath the import statement in QML but i have no code completion.

              When i run the project all works fine.

              I am a little bit confused.

              Have anybody a solution?

              Thanks.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lselle
                wrote on last edited by
                #11

                Hello.

                I have exact the same problem.

                At this time i have create a QtQuick Plugin project and register my singletontype in this project. In my other project i have add QML_IMPORT_PATH = PathToMyQtQuickPlugin and now i have no red line underneath the import statement in QML but i have no code completion.

                When i run the project all works fine.

                I am a little bit confused.

                Have anybody a solution?

                Thanks.

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  xenotrax
                  wrote on last edited by
                  #12

                  I still have no solution. A restart of Qt did not solve the problem

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xenotrax
                    wrote on last edited by
                    #13

                    I still have no solution. A restart of Qt did not solve the problem

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lselle
                      wrote on last edited by
                      #14

                      Hello.

                      I have seen the following video from Qt Developer Days 2013 about Qt Quick Controls.
                      https://www.youtube.com/watch?v=JLLIYysXsj8

                      In this Video shows Jens Bache-Wiig live coding with Qt Quick Controls and bind controls to a c++ class. This c++ class has he registered as singleton and he has the same problem. He has no auto complete in Qt Creator.

                      I think at this time, we must life with this problem. I hope that this feature comes in Qt Creator in a future release.

                      @xenotrax: Thanks for reply.

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lselle
                        wrote on last edited by
                        #15

                        Hello.

                        I have seen the following video from Qt Developer Days 2013 about Qt Quick Controls.
                        https://www.youtube.com/watch?v=JLLIYysXsj8

                        In this Video shows Jens Bache-Wiig live coding with Qt Quick Controls and bind controls to a c++ class. This c++ class has he registered as singleton and he has the same problem. He has no auto complete in Qt Creator.

                        I think at this time, we must life with this problem. I hope that this feature comes in Qt Creator in a future release.

                        @xenotrax: Thanks for reply.

                        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