QML Plugin. Returning a QScriptValue without using private headers.
-
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?
-
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?
-
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.
-
You can add your own custom roles and they will be accessible from qml. It it will not help?
-
I'm sorry, I'm not being clear.
- I created a Custom Model
- I have custom defined roles
- The model works with all standard QML views (grid, list, etc)
- 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?
-
Hm, I think that simple returning QVariant or QString should work well, don't it?
-
[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