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

Unexpected and inconsistent overload resolution when calling C++ from QML

    XMLWordPrintable

Details

    • 22eaa614e (dev), 6c8cf863b (6.6), c9f7ba0f2 (tqtc/lts-6.5), 2d8fb3169 (tqtc/lts-6.2)

    Description

      Consider the following C++ class:

      class TestThing : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
          QML_SINGLETON
      public:
          explicit TestThing(QObject *parent = nullptr);
          Q_INVOKABLE void foo(const QString &bla);
          Q_INVOKABLE void foo(TestThing *a);
      }; 

      And the QML code

      Item {
          property QtObject obj: TestThing;
          Component.onCompleted: TestThing.foo(obj)
      } 

      Full code at https://invent.kde.org/nicolasfella/qmloverload

       

      The QML code will call the QString overload instead of the TestThing * overload, which I find rather unexpected.

      Changing the type of the obj property from QtObject to TestThing makes it choose the TestThing overload (as I expect).

      What's even more confusing is that when I remove the declarative type registration macros and rely on runtime registration then the behavior changes and the TestThing overload is chosen.

      I found this behavior in KDE Plasma after porting to declarative type registration cause a regression.

      Qt 6.6

      qtbase f4b91d595ce0ec69b01848bfe68d468c63d20d5a

      qtdeclarative e1fbca3d7338c63672af7224e6b70ad11aea9332

      Attachments

        For Gerrit Dashboard: QTBUG-117922
        # Subject Branch Project Status CR V

        Activity

          People

            ulherman Ulf Hermann
            nicolasfella Nicolas Fella
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews