PDA

View Full Version : Issue with c++ model



pkl
24th October 2014, 09:36
I have class:


class diagSys : public QObject
{
Q_OBJECT
...
Q_PROPERTY(QList<QObject *> periphList READ periphList NOTIFY sysConfigChanged)
...


periphList is list of objects:


class BriefBlockInfoObject : public QObject
{
Q_OBJECT

Q_PROPERTY(char address READ address WRITE setAddress /*NOTIFY typeChanged*/)
Q_PROPERTY(int id READ id WRITE setId /*NOTIFY textChanged*/)
Q_PROPERTY(QString name READ name WRITE setName /*NOTIFY textChanged*/)
...


I wanna use periphList as model in qml. I export diagSys object:


view->rootContext()->setContextProperty(QLatin1String("DIAG"),
&diag);


From qml I use this:


Repeater {
...
model: DIAG.dispList
...

But It causes an error "ReferenceError: Can't find variable: name/address/id"

But if I export like this:


view->rootContext()->setContextProperty("dispModel",
QVariant::fromValue(diag.dispList()));

and use as model dispModel:


Repeater {
...
model: dispModel
...

it perfectly works.

How can I use as model property value of object, not object itself?

wysota
24th October 2014, 15:12
Either declare the property as QVariant type (which will effectively make it equivalent to your second attempt) or do it the proper way and declare the property as QQmlListProperty type. This needs more work to be implemented properly but will give you full capabilities of a list property (e.g. possibility to add/remove entries from within QML).

anda_skoa
27th October 2014, 02:32
I would also expect this to work.
Your QML snippet uses a different property name than your C++, is this a typo?

Cheers,
_

pkl
27th October 2014, 09:55
The problem is solved. Just need to use modelData.name/modelData.address/modelData.id.