Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QML Plugin. Returning a QScriptValue without using private headers.

    QML and Qt Quick
    3
    8
    3940
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      Gary_ last edited by

      I created my own list model, and I wish to return a QScriptValue to QML. QDeclarativeXmlListModel has the "get" function that I am trying to mimic. However, I do not wish to use any private header methods.

      The QDeclarativeXmlListModel code segment is as follows:

      @
      QScriptValue QDeclarativeXmlListModel::get(int index) const
      {
      Q_D(const QDeclarativeXmlListModel);

      QScriptEngine *sengine = QDeclarativeEnginePrivate::getScriptEngine(qmlContext(this)->engine());
      if (index < 0 || index >= count())
          return sengine->undefinedValue();
      
      QScriptValue sv = sengine->newObject();
      for (int i=0; i<d->roleObjects.count(); i++)
          sv.setProperty(d->roleObjects[i]->name(), qScriptValueFromValue(sengine, d->data.value(i).value(index)));
      return sv;   
      

      }
      @

      My code looks like the following, however, I do not know how to get the current Script Engine. I am creating my own QScriptEngine as an example (I know this does not work).

      @
      QScriptEngine engine;

      if (index < 0 || index >= rowCount())
          return engine.undefinedValue();
      
      QScriptValue value = engine.newObject();
      
      QHash<int, QByteArray>::const_iterator itr;
      
      for (itr = roleNames().constBegin(); itr != roleNames().constEnd(); ++itr) {
          value.setProperty(
                  QString(itr.value()),
                  qScriptValueFromValue(&engine, data(this->index(index), itr.key()).toString()));
      }
      
      return value;
      

      @

      What is the proper way?

      1 Reply Last reply Reply Quote 0
      • D
        DenisKormalev last edited by

        Hm, can you provide use case why do you need such way and can't make it via data() method or via properties of model?

        1 Reply Last reply Reply Quote 0
        • G
          Gary_ last edited by

          Using QScriptValue:

          The get method will iterate all roles and create a property for each role name.

          @
          value = addressBookModel.get(index);

          value.name
          value.address
          value.city
          value.state
          value.zip
          ..
          ..
          @

          Using a exported method:

          The method will have to look up the role index by role name for each call.

          @
          addressBookModel.get(index, "name");
          addressBookModel.get(index, "address");
          addressBookModel.get(index, "city");
          addressBookModel.get(index, "state");
          addressBookModel.get(index, "zip");
          ..
          @

          We went with the first (QScriptValue) approach because we access many properties on a single indexed item. (Think of the addressBook having 20 properties and we access each one).

          Also, it is the way that the built-in models access role names. We used role names, because it is how delegates pull properties from models to draw views.

          1 Reply Last reply Reply Quote 0
          • D
            DenisKormalev last edited by

            You can add your own custom roles and they will be accessible from qml. It it will not help?

            1 Reply Last reply Reply Quote 0
            • G
              Gary_ last edited by

              I'm sorry, I'm not being clear.

              1. I created a Custom Model
              2. I have custom defined roles
              3. The model works with all standard QML views (grid, list, etc)
              4. When painting a view with a delegate, the delegate correctly uses my custom roles

              What is the most efficient way I access the value of these custom roles programmatically?

              I started by looking at Qt's source, and their standard models such as their qdeclarativexmllistmodel.cpp source. The function in my original questions is a snippet from that source. It is the exact way I wish to access my model. However, it uses private classes/headers to achieve this. I assume it is so they can be flexible with the engine logic without impacting the public SDK.

              Is it possible to safely implement their approach using the public SDK? If not, what is the most efficient way to do it?

              1 Reply Last reply Reply Quote 0
              • D
                DenisKormalev last edited by

                Hm, I think that simple returning QVariant or QString should work well, don't it?

                1 Reply Last reply Reply Quote 0
                • G
                  Gary_ last edited by

                  I need the return value to be an associative array.

                  1 Reply Last reply Reply Quote 0
                  • M
                    mbrasser last edited by

                    [quote author="Gary_" date="1287690827"]It is the exact way I wish to access my model. However, it uses private classes/headers to achieve this. I assume it is so they can be flexible with the engine logic without impacting the public SDK.[/quote]

                    That's exactly right. The bug that tracks this is:
                    "QTBUG-11942":http://bugreports.qt.nokia.com/browse/QTBUG-11942

                    [quote author="Gary_" date="1287690827"]Is it possible to safely implement their approach using the public SDK? If not, what is the most efficient way to do it?[/quote]

                    There is "this thread":http://lists.trolltech.com/pipermail/qt-qml/2010-September/001313.html that mentions two approaches to accessing the engine (one using private headers). Alternatively, what about returning a QDeclarativePropertyMap instead of a QScriptValue (may depend on planned ownership of this value)?

                    Regards,
                    Michael

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post