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

wrong example of using create function

    XMLWordPrintable

Details

    Description

      https://doc.qt.io/qt-6/qqmlengine.html#QML_SINGLETON
      the example using create function is missing an important piece.
      the create function only be triggered when the default constructor is detected.
      but the code example makes the user missing understand and when try to run it the create function will never get called and without any error or warning, it requires the user to delete the constructor like

      class MySingleton : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
          QML_SINGLETON
          // Q_PROPERTY( ... )
      
      public:
          MySingleton (QObject* object) {
               // replace the default constructor to trigger create function
          }
      
          static MySingleton *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
          {
              MySingleton *result = nullptr;
              // Create the object using some custom constructor or factory.
              // The QML engine will assume ownership and delete it, eventually.
              return result;
          }
      
          // members, Q_INVOKABLE functions, etc
      };
      

      either fix the example code or suggest changing the specification if create is defined call it first instead of the constructor.

      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
            jiu shanheng jiu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes