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. [Solved] C++ Signals for QML Layer
Forum Updated to NodeBB v4.3 + New Features

[Solved] C++ Signals for QML Layer

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 9.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.
  • S Offline
    S Offline
    Schneidi
    wrote on last edited by
    #1

    Hi,

    I have a question about the signal connection between my C++ logic layer and my QML visual Layer.

    What I need is to connect my interface class with the QML layer, I found some tutorials about that topic.
    The answer seems to be the

    qmlRegisterType<QmlInterface >("QmlInterface ", 1, 0, "Interface ");

    statement.
    Ok so far so good, but in this examples I'm missing one detail.

    Lets take the QmlInterface class. In the example the QmlInterface class is just one independent class with no connection
    to other classes or the rest of the C++ logic.

    In my current project I have a QmlInterface class which is designed to send diffenrent events and informations to my QML layer.
    This QmlInterface needs a pointer to my CoreEngine which is handling the different events. But I can't figure out how I can
    use signals of my QmlInterface class in the QML layer.

    Lets say this is a pretty primitive variant of my QmlInterface class. I instantiate this class so far to set an index from a CoreEngine
    when the index is changed I want to emit a Signal indexChanged() so that the QML layer know that the index was changed.

    This class should work for the qmlRegisterType approach.
    But my QmlInterface needs a pointer to the CoreEngine.

    Until now the QmlInterface class has a parameter CoreEngine *core for the interaction with the core of my app.
    But this isn't working when I use the qmlRegisterType().

    So what can I do to realize this connection from CoreEngine-> to QmlInterface -> to QML-layer ?

    I hope this isn't to confusing and you nearly understand whats my issue.

    @

    class QmlInterface : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(int index READ index NOTIFY indexChanged)

    public:
    QmlInterface(QObject *parent=0) : QObject(parent)
    {
    /some stuff/
    this->m_index = 1;
    }

     int index() const { return this->index; }
    
     void changeIndex(int index)
     {
         this->m_index = index;
         emit indexChanged();
     }
    

    signals:
    void indexChanged();

    private:
    int m_index;
    };

    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      Here are a couple things you could try:

      • Make the CoreEngine available via a static function, and call that function in the QmlInterface constructor
        @QmlInterface(QObject *parent=0) : QObject(parent)
        {
        m_coreEngine = CoreEngine::instance();
        }@
      • Use setContextProperty() to expose the object, rather than adding it as an element in the QML tree. See "http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#structured-data":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#structured-data for more details on this approach. (you'd then need to use the Connections element to connect to signals.)

      Michael

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Schneidi
        wrote on last edited by
        #3

        Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.

        But I don't understand your idea with the static instance() method.
        What should the instance() method return ? a pointer to the Core ?

        But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.

        Maybe you can tell me what instance() should do in your opinion.

        Jan

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          [quote author="Schneidi" date="1283522910"]Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.[/quote]

          How did you try to access the signals? Assuming you exposed your class as "myInterface", and it had a signal named "mySignal()", the following should work:

          @Connections {
          target: myInterface
          onMySignal: ...
          }@

          [quote author="Schneidi" date="1283522910"]But I don't understand your idea with the static instance() method.
          What should the instance() method return ? a pointer to the Core ?

          But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.

          Maybe you can tell me what instance() should do in your opinion.[/quote]

          Sorry, I should have been more explicit. I was thinking you could use the Singleton pattern, if it made sense in the context of your application. So something like:

          @CoreInstance *CoreInstance::instance()
          {
          static CoreInstance *myInstance = 0;
          if (!myInstance)
          myInstance = new CoreInstance();
          return myInstance;
          }@

          Michael

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Schneidi
            wrote on last edited by
            #5

            Hi,

            thank you Micheal the Connections {} utility was exactly what I needed.

            The Singleton isn't really suitable for this case.

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yan_
              wrote on last edited by
              #6

              Hi.

              Like qml use QtSCript you can use connect function

              @myObject.mySignal.connect(...)@

              "http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections":http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections

              Normally it's works.

              [edit: fixed link / $chetankjain]

              "QExtend":http://www.developpez.net/forums/d906429/c-cpp/bibliotheques/qt/naissance-projet-qextend/
              "QEXtend Video":http://www.youtube.com/user/QEx...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Schneidi
                wrote on last edited by
                #7

                Thanks for the hint.

                I didn't use QtScript yet maybe I should take a look at it. ^^

                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