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

Assign direct to QML (through setContextProperty) works, as function doesn't.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 4.8.3
    • None
    • Linux x64 (Archlinux)

    Description

      Hi,

      I raised this on the mailing list as well: http://lists.qt-project.org/pipermail/interest/2012-November/004650.html

      Thus far no solution has been found and i'm beginning to believe that it might be a bug. So i'm reporting the same issue here.

      Lets take this (pseudo code) as example.
      I made a custom QStringListModel class and calles it PathModel:

      pathmodel.h
      class PathModel : public QStringListModel
      {
      Q_OBJECT
      public:
      explicit PathModel(QObject *parent = 0);
      };

      Then i made a custom "utils" class where i just make some random
      functionality available. Like so:

      util.h
      class Util : public QObject
      {
      Q_OBJECT
      public:
      ....
      Q_INVOKABLE PathModel* pathModel();
      Q_INVOKABLE QString testString()

      { return "some_test_text"; }

      ....
      };

      Then i have the main file where i create the QML context and load a
      QML file. Just the default stuff when you make a QML application
      through QtCreator. It looks like this:

      Q_DECL_EXPORT int main(int argc, char *argv[])
      {
      QScopedPointer<QApplication> app(createApplication(argc, argv));

      QmlApplicationViewer viewer;
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      viewer.setMainQmlFile(QLatin1String("qml/Porpoise/main.qml"));
      viewer.showExpanded();

      return app->exec();
      }

      The above helps understanding the context. In the above you can
      already see that i want to send the Util class to QML. I do that like
      so:

      Util util;
      viewer.rootContext()->setContextProperty("Util", &util);

      Now in QML i call:
      Util.testString()
      Reply: "some_test_text"

      That is es expected. Nothing wrong.

      Now if i call:
      Util.pathModel()
      Reply: undefined

      UNDEFINED..! Why?

      If i do this:
      viewer.rootContext()->setContextProperty("PathModel", Util.pathModel());

      and then call it in QML like so:
      PathModel
      Reply: QStringListModel(0x2072430)

      QStringListModel is OK since PathModel inherits from that. So
      assigning it directly to QML works, but assigning it through some
      other class doesn't. Can anyone tell me why that's the case? And more
      importantly, how can i fix this?

      Note: i cannot do qRegisterMetaType on the PathModel since it's class
      that inherits from a class that forbids copying the data (copy
      constructor stuff).

      Cheers,
      Mark

      Attachments

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

        Activity

          People

            martinj Martin Jones
            markg85 Mark
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes