Signal fromm Cpp to QML

  • 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 :


    #include "Source.h"
    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        QQuickView viewer;
        Source source;
        viewer.rootContext()->setContextProperty("source", &source);
        return app.exec();

    source hinerits from QObject :


    #ifndef SOURCE_H
    #define SOURCE_H
    #include <QObject>
    class Source : public QObject
        explicit Source(QObject *parent = 0);
            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: {

    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....

  • Hello,

    I don't know if this is the best way to do it, but this tutorial works perfectly :

  • @Zhitoune I assume you are working close to this tutorial:

    But what is the line number where the error occurs?

    And what does N(256) do?

  • Thanks for our answers,

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



    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.

  • This post is deleted!

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

    Connections: {

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.