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. Can't use a c++ signal in QML

Can't use a c++ signal in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.5k 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.
  • J Offline
    J Offline
    JorgeCosta87
    wrote on last edited by JorgeCosta87
    #1

    I have tried to receive a signal from a c++ class to QML, without any success. I have followed the tutorial provided by QT
    [http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html](link url).

    My code:
    The main class (c++)
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    Manager manager;
    QQmlApplicationEngine engine;
    
    qmlRegisterType<Manager>("costa.manager", 1, 0, "Manager");
    
    engine.rootContext()->setContextProperty("manager",&manager);
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;
    
    return app.exec();
    

    }

    Class Manager .h with the signal:

    class Manager : public QObject
    {
    Q_OBJECT

    public:
    explicit Manager(QObject *parent = nullptr);

    signals:
    void isVechicleConnected(const bool &b);

    ...
    }

    Class Manager .cpp

    void Manager::isConnected(const bool &b)
    {
    qDebug() << "signal"; //to check if i'm sendding the signal
    emit isVechicleConnected(b);
    }

    QML main:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.Controls.Material 2.0
    import costa.manager 1.0

    ApplicationWindow {

    ...
    Manager{
    onIsVechicleConnected: {
    console.log("Connected!!!" + b)
    }
    }
    ...
    }

    How to fix it ?

    thanks !

    E 1 Reply Last reply
    0
    • J JorgeCosta87

      I have tried to receive a signal from a c++ class to QML, without any success. I have followed the tutorial provided by QT
      [http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html](link url).

      My code:
      The main class (c++)
      int main(int argc, char *argv[])
      {
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      QGuiApplication app(argc, argv);

      Manager manager;
      QQmlApplicationEngine engine;
      
      qmlRegisterType<Manager>("costa.manager", 1, 0, "Manager");
      
      engine.rootContext()->setContextProperty("manager",&manager);
      engine.load(QUrl(QLatin1String("qrc:/main.qml")));
      if (engine.rootObjects().isEmpty())
          return -1;
      
      return app.exec();
      

      }

      Class Manager .h with the signal:

      class Manager : public QObject
      {
      Q_OBJECT

      public:
      explicit Manager(QObject *parent = nullptr);

      signals:
      void isVechicleConnected(const bool &b);

      ...
      }

      Class Manager .cpp

      void Manager::isConnected(const bool &b)
      {
      qDebug() << "signal"; //to check if i'm sendding the signal
      emit isVechicleConnected(b);
      }

      QML main:

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3
      import QtQuick.Controls.Material 2.0
      import costa.manager 1.0

      ApplicationWindow {

      ...
      Manager{
      onIsVechicleConnected: {
      console.log("Connected!!!" + b)
      }
      }
      ...
      }

      How to fix it ?

      thanks !

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @JorgeCosta87 Now you create a new object of type Manager in main.qml. Remove that. You don't need to register the type. Instead, the object named 'manager' is a context property and is globally available in your QML code. You need to use Connections to catch the signal:

      Connections: {
      target: manager
      onIsVehicleConnected: {}
      }
      

      BTW, the signal is weirdly named. E.g. QGamepad has the "connected" property and "connectedChanged" signal, and "onConnectedChanged" would be the event handler in QML. "isConnected", judged by its name, should be a query method which doesn't send a signal. I recommend making "connected" a real property. I have something like this in a project:

      ...
      Q_OBJECT
      Q_PROPERTY(bool connectedStatus MEMBER m_connected NOTIFY connectedStatusChanged)
      ...
      private: bool m_connected;
      ...
      signals: void connectedStatusChanged(bool connected);
      
      J 1 Reply Last reply
      1
      • E Eeli K

        @JorgeCosta87 Now you create a new object of type Manager in main.qml. Remove that. You don't need to register the type. Instead, the object named 'manager' is a context property and is globally available in your QML code. You need to use Connections to catch the signal:

        Connections: {
        target: manager
        onIsVehicleConnected: {}
        }
        

        BTW, the signal is weirdly named. E.g. QGamepad has the "connected" property and "connectedChanged" signal, and "onConnectedChanged" would be the event handler in QML. "isConnected", judged by its name, should be a query method which doesn't send a signal. I recommend making "connected" a real property. I have something like this in a project:

        ...
        Q_OBJECT
        Q_PROPERTY(bool connectedStatus MEMBER m_connected NOTIFY connectedStatusChanged)
        ...
        private: bool m_connected;
        ...
        signals: void connectedStatusChanged(bool connected);
        
        J Offline
        J Offline
        JorgeCosta87
        wrote on last edited by
        #3

        @Eeli-K said in Can't use a c++ signal in QML:

        onConnectedChanged

        Thanks a lot!
        True, makes much more sense that name.

        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