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);
...
};
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);
...
};
To copy to clipboard, switch view to plain text mode
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
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:
{
QString type
= model
->data
(...
);
// model would be "mymodel" from QML int amount = model->data(...);
// paint a graphic using the "type" and "amount" variables
}
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
}
To copy to clipboard, switch view to plain text mode
Bookmarks