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

Allow using const pointers as parameters of C++ functions called by QML

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • P2: Important
    • 6.2
    • None
    • Core: Object Model
    • None
    • 8e904d9c8b36af4310070aa68ddb04cf33520b52 (qt/qtdeclarative/dev)

    Description

      I have an invokable function in C++ that should be const with const parameters:

      Q_INVOKABLE QPoint findClosestAvailableTilePos(const QPoint &tilePos, const SceneItemComponent *sceneItemComponent = nullptr) const;
      

      I register it with QML like this:

      qmlRegisterUncreatableType<SceneItemComponent>("Isle", 1, 0, "SceneItemComponent", "SceneItemComponent should not be created in QML");
      

      If I try to call this with a SceneItemComponent object in JavaScript, the parameter simply becomes null in C++. Changing the parameter to a non-const SceneItemComponent* is a workaround, but not that nice, because it means that everywhere I use this function in C++ has to be adapted (and sometimes const_casted).

      It would be better if it were possible to use the const type in QML and just get an error if any non-const operations (getters, setters, invokables) were performed on it, if that's even possible. I don't know if e.g. moc even stores constness of functions, or whether such a change to the QML engine would affect performance adversely... but it would be nice to have.

      Attachments

        Issue Links

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

          Activity

            People

              fabiankosmale Fabian Kosmale
              mitch_curtis Mitch Curtis
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes