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. Signal fromm Cpp to QML
Forum Updated to NodeBB v4.3 + New Features

Signal fromm Cpp to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 2.8k 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.
  • Z Offline
    Z Offline
    Zhitoune
    wrote on last edited by
    #1

    Dear All,

    I'm looking to intercept a signal emited from a Cpp object in a QML object, I'm trying to achieve this setting my cpp file as a context Property :

    main.cpp

    #include "Source.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQuickView viewer;
        Source source;
        viewer.rootContext()->setContextProperty("source", &source);
        viewer.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
        viewer.show();
        return app.exec();
    }
    

    source hinerits from QObject :

    source.h

    #ifndef SOURCE_H
    #define SOURCE_H
    #include <QObject>
    
    class Source : public QObject
    {
        Q_OBJECT
    public:
        explicit Source(QObject *parent = 0);
        signals:
            void dataChanged();
    };
    #endif // SOURCE_H
    
    

    Source.cpp :

    #include "source.h"
    
    Source::Source(QObject *parent) : QObject(parent), N(256)
    {    
        emit dataChanged();    
    }
    

    and in my QML file I use Connections :

    import QtQuick 2.7
    
    Item {
        id: rootI
        width: 1000
        height: 600
        Connections: {
            target: source
            onDataChanged: {
                console.log("NewData")
            }
        }
    }
    

    And on execution I get : "Invalid attached object assignment"

    I have looked for the solutions in a lot of document but I still can't find what's wrong with that code....

    C 1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zhitoune
      wrote on last edited by
      #7

      Yup it works fine without the column.
      thanks a lot and sorry for this oversight!

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fheanor
        wrote on last edited by
        #2

        Hello,

        I don't know if this is the best way to do it, but this tutorial works perfectly :
        https://andrew-jones.com/blog/qml2-to-c-and-back-again-with-signals-and-slots/

        1 Reply Last reply
        2
        • Z Zhitoune

          Dear All,

          I'm looking to intercept a signal emited from a Cpp object in a QML object, I'm trying to achieve this setting my cpp file as a context Property :

          main.cpp

          #include "Source.h"
          
          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
              QQuickView viewer;
              Source source;
              viewer.rootContext()->setContextProperty("source", &source);
              viewer.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
              viewer.show();
              return app.exec();
          }
          

          source hinerits from QObject :

          source.h

          #ifndef SOURCE_H
          #define SOURCE_H
          #include <QObject>
          
          class Source : public QObject
          {
              Q_OBJECT
          public:
              explicit Source(QObject *parent = 0);
              signals:
                  void dataChanged();
          };
          #endif // SOURCE_H
          
          

          Source.cpp :

          #include "source.h"
          
          Source::Source(QObject *parent) : QObject(parent), N(256)
          {    
              emit dataChanged();    
          }
          

          and in my QML file I use Connections :

          import QtQuick 2.7
          
          Item {
              id: rootI
              width: 1000
              height: 600
              Connections: {
                  target: source
                  onDataChanged: {
                      console.log("NewData")
                  }
              }
          }
          

          And on execution I get : "Invalid attached object assignment"

          I have looked for the solutions in a lot of document but I still can't find what's wrong with that code....

          C Offline
          C Offline
          c64zottel
          wrote on last edited by c64zottel
          #3

          @Zhitoune I assume you are working close to this tutorial:
          http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

          But what is the line number where the error occurs?

          And what does N(256) do?

          1 Reply Last reply
          1
          • Z Offline
            Z Offline
            Zhitoune
            wrote on last edited by
            #4

            Thanks for our answers,

            N(256) is just a remnant of the full code that I cleared for readability.
            the error happens at the :

            Connections:{
            

            line.

            Yes I'm trying to emulate the solution based on the doc.qt without succes so far.

            thanks Fheanor, I'll try this solution but I would like to understand why this method is not working.

            C 2 Replies Last reply
            0
            • Z Zhitoune

              Thanks for our answers,

              N(256) is just a remnant of the full code that I cleared for readability.
              the error happens at the :

              Connections:{
              

              line.

              Yes I'm trying to emulate the solution based on the doc.qt without succes so far.

              thanks Fheanor, I'll try this solution but I would like to understand why this method is not working.

              C Offline
              C Offline
              c64zottel
              wrote on last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • Z Zhitoune

                Thanks for our answers,

                N(256) is just a remnant of the full code that I cleared for readability.
                the error happens at the :

                Connections:{
                

                line.

                Yes I'm trying to emulate the solution based on the doc.qt without succes so far.

                thanks Fheanor, I'll try this solution but I would like to understand why this method is not working.

                C Offline
                C Offline
                c64zottel
                wrote on last edited by
                #6

                @Zhitoune Try to remove the colon behind Connections. It must be
                Connections {

                not
                Connections: {

                1 Reply Last reply
                2
                • Z Offline
                  Z Offline
                  Zhitoune
                  wrote on last edited by
                  #7

                  Yup it works fine without the column.
                  thanks a lot and sorry for this oversight!

                  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