PDA

View Full Version : Using a QML model from a QDeclarativeItem subclass



humbleguru
2nd January 2011, 17:59
Hi! I'm creating a QDeclarativeItem subclass:


class MyGraphic : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(????? model READ model SET model)

public:
MyGraphic(QDeclarativeItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
...
};

I would like to use it in QML like this:


Rectangle {
ListModel {
id: mymodel
ListElement { type: "Oranges"; amount: 35 }
ListElement { type: "Apples"; amount: 27 }
ListElement { type: "Peaches"; amount: 12 }
}

MyGraphic {
width: 300; height: 200
model: mymodel
}
}

There are plenty of examples on how to make a C++ model available in the QML world but I have found nothing on how to access a QML model from C++.

How can I access mymodel from MyGraphic (the C++ class)? Should I use QAbstractItemModel* directly in the Q_PROPERTY?

I mean something like:


void MyGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QString type = model->data(...); // model would be "mymodel" from QML
int amount = model->data(...);
// paint a graphic using the "type" and "amount" variables
}

humbleguru
3rd January 2011, 12:07
I got answered in the Qt forums (http://developer.qt.nokia.com/forums/viewthread/2947/): Seems like it's not possible yet.