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

Cannot use QSharedPointer with QJSEngine

    XMLWordPrintable

Details

    Description

      Carrying on from QTBUG-35386, I'm trying to get the same thing to work with QJSEngine, since it's unlikely to be fixed in QScriptEngine.

      #include <QtCore>
      #include <QtQml>
      
      class Object : public QObject
      {
          Q_OBJECT
      public:
          Q_PROPERTY(int x READ x WRITE setX)
      
          Object(int x) : mX(x) {}
      
          int x() const { return mX; }
          void setX(int x) { mX = x; }
      private:
          int mX;
      };
      
      class Context : public QObject
      {
          Q_OBJECT
      public:
          Q_PROPERTY(QSharedPointer<Object> object READ object)
      
          Context(QSharedPointer<Object> object) : mObject(object) {}
      
          QSharedPointer<Object> object() const { return mObject; }
      
      private:
          QSharedPointer<Object> mObject;
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Q_UNUSED(a);
      
          QJSEngine engine;
      
          QSharedPointer<Object> objectPtr(new Object(5));
          Context context(objectPtr);
          engine.globalObject().setProperty("context", engine.newQObject(&context));
      
          qDebug() << engine.evaluate("context.object.x").toString();
      
          return 0;
      }
      
      #include "main.moc"
      

      Output:

      "undefined"

      Attachments

        Issue Links

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

          Activity

            People

              shausman Simon Hausmann
              mitch_curtis Mitch Curtis
              Votes:
              2 Vote for this issue
              Watchers:
              11 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes