Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-120951

connecting signal of aliased property in QML to C++ slot doesn't work

    XMLWordPrintable

Details

    • Windows

    Description

      #include <QGuiApplication>
      #include <QMetaObject>
      #include <QQmlApplicationEngine>
      
      class Handler : public QObject {
          Q_OBJECT
      public slots:
          void handle()
          {
              qDebug() << "received";
          }
      };
      
      auto findMetaMethod(QObject *object, const char *signature) -> QMetaMethod
      {
          const auto mo = object->metaObject();
          return mo->method(mo->indexOfMethod(signature));
      }
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          engine.loadData(R"(
      import QtQuick
      
      Window {
          width: 200; height: 200; visible: true
          property alias value: mouse.value
          MouseArea {
              id: mouse
              anchors.fill: parent
              property bool value: false
              onClicked: {
                  value = !value
              }
              Text {
                  anchors.centerIn: parent
                  text: mouse.value
              }
          }
          // onValueChanged: {} // existence of QML handler makes the C++ connection work
      }
      )");
          const auto sender = engine.rootObjects().constFirst();
          const auto signal = findMetaMethod(sender, "valueChanged()");
          Handler handler;
          const auto slot = findMetaMethod(&handler, "handle()");
          qDebug() << "signal" << signal.isValid();
          qDebug() << "slot" << slot.isValid();
          qDebug() << "connect" << !!QObject::connect(sender, signal, &handler, slot);
      
          return app.exec();
      }
      
      #include "main.moc"
      

      Here, I have tried to make connection between signal for aliased property in QML and C++ slot but the slot never invoked even though the returned connection object is truthy.

      However, if I add any signal handler in QML, C++ slot suddenly works.

      I have attached a webp which shows the problem.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qtqmlteam Qt Qml Team User
            bylee Byoung-young Lee
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes