kinetic-declarative plasma integration

aaron.kennedy at aaron.kennedy at
Fri May 15 03:52:56 CEST 2009

Hi Marco,

On 15/05/09 7:10 AM, "ext Marco Martin" <notmart at> wrote:

>>> * if one wants to create QML items dynamically, e.g. when a DataEngine
>>> returns a source create a new QML item and load it, how does that work
>>> exactly? can that be done from a JavaScript file?
>> We have a couple of ways of dealing with "lists" of things (which sounds
>> like what the sources returned from a dataengine conceptually are).  All of
>> our view classes can take sets of things, and we also have a generalized
>> "Repeater" element for handling other cases.
> i'm indeed playing with views now.
> since the qml documentation appears still not there i'm going by attempts:
> what classes are for views? until now i've found ListView and GridView,
> anything else?
> also, is it possible to do an horizontal list view?
> but the real question is: is it possible somehow to plug a traditional qt
> model into it?

The documentation is still a work in progress.  In the specific case of the
views available, however, I think its ok.  From the main page, navigate to
"Declarative Module"/"Qml Elements" and then look under the "Views" heading.

Incase you haven't generated the documentation, but want to get going fast,
here are the specific answers to your questions :)

You can set a ListView into horizontal mode by setting the "orientation"
property, like this:

ListView {
    orientation: "Horizontal"

You can plug a QAbstractItemModel Qt model into our views, with one small
extra step.  Because we expose each model role as a JavaScript property, not
an integer enumeration value, we need a way to map between the role ints in
your C++ model and textual representations.  We've added a "void
QAbstractItemModel:: setRoleNames(const QHash<int,QByteArray> &roleNames)"
method to let you specify this mapping.

So, to hook this all up:

// Setup role mapping
QHash<int, QByteArray> roleNames;
roleNames.insert(1023, "myRoleName");

// Bind the C++ model instance into a QML context
QmlContext *context = new QmlContext(engine->rootContext());
context->setContextProperty("myModel", static_cast<QObject *>(myModel));

// This is the example QML text that we'll instantiate
#define QML_TEXT "Rect { color: "white"; ListView { anchors.fill: parent;
delegate: Text { text: myRoleName } model: myModel } }"

// Create a QML component that uses the model
QmlComponent component(engine, QML_TEXT);

This will give you a fairly boring list containing the text of the
"myRoleName" role for each entry in your model.


More information about the Plasma-devel mailing list