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. Not able to connect c++ signal to qml slot using QML connections
QtWS25 Last Chance

Not able to connect c++ signal to qml slot using QML connections

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 4.9k Views
  • 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.
  • C Offline
    C Offline
    cgoma
    wrote on last edited by
    #1

    I am unable to connect C++ signal to QML slot using QML Connections

    Below are the code snippets.

    I have created my class like below.

    i.e. connection.h
    @
    #ifndef CONNECTION_H
    #define CONNECTION_H

    #include <QObject>

    class connection : public QObject
    {
    Q_OBJECT
    public:
    explicit connection(QObject *parent = 0);

    signals:
    void mySignal();

    public slots:
    Q_INVOKABLE void mySlot();

    };
    @

    connection.cpp

    @
    #include "connection.h"
    #include<QDebug>

    connection::connection(QObject *parent) :
    QObject(parent)
    {
    }

    void connection::mySlot()
    {
    qDebug() << "I have got the Signal" <<'\n';
    emit mySignal();
    }
    @

    main.cpp is below.

    @
    #include <QtGui/QGuiApplication>
    #include <QQmlContext>
    #include "qtquick2applicationviewer.h"
    #include "connection.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    connection obj;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/stateTransistion/main.qml"&#41;&#41;;
    
    viewer.rootContext()->setContextProperty("connectionHandler", &obj);
    viewer.showExpanded();
    
    return app.exec&#40;&#41;;
    

    }
    @

    main.qml is as below

    @
    import QtQuick 2.0

    Rectangle {
    id: signal
    width: 200; height: 200
    border.color: "black"

    MouseArea {
            anchors.fill: parent
            onClicked: {
                connectionHandler.mySlot()
            }
        }
    
    Connections
    {
        target: connectionHandler
        onMySignal:console.log("I have got the signal emitted from C++")
    }
    

    }
    @

    Whenever I ran my application, I will get below error

    QML Connections: Cannot assign to non-existent property "onMySignal"
    ReferenceError: connectionHandler is not defined

    If I have commented the Connections in main.qml then I am able to call mySlot() (i.e. Line number 11 in main.qml) using connectionHandler.
    But when I uncommented Connections then i am getting below error

    QML Connections: Cannot assign to non-existent property "onMySignal"
    ReferenceError: connectionHandler is not defined

    I very novice in Qt.
    Please help me to resolve these errors

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Load the QmL file after exposing the object. Also get engine from viewer and get context. Set your variables in this context.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cgoma
        wrote on last edited by
        #3

        It is working fine.
        Thanks for your reply.
        But I am not getting the difference between loading the QML file after exposing the object and loading the QML file before exposing the object?

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Ok. You are using your registered object inside QmL. When you load QmL it looks for this object. How does it know about your object ?. You need to register before loading.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          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