January 3, 2011

YetAnotherNick YetAnotherNi..
Lab Rat
10 posts

Using a QML model from a QDeclarativeItem subclass

 

Hi! I’m creating a QDeclarativeItem subclass:

  1. class MyGraphic : public QDeclarativeItem
  2. {
  3.     Q_OBJECT
  4.     Q_PROPERTY(????? model READ model SET model)
  5.  
  6. public:
  7.     MyGraphic(QDeclarativeItem *parent = 0);
  8.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
  9.     ...
  10. };

I would like to use it in QML like this:

  1. Rectangle {
  2.     ListModel {
  3.         id: mymodel
  4.         ListElement { type: "Oranges"; amount: 35 }
  5.         ListElement { type: "Apples"; amount: 27 }
  6.         ListElement { type: "Peaches"; amount: 12 }
  7.     }
  8.  
  9.     MyGraphic {
  10.         width: 300; height: 200
  11.         model: mymodel
  12.     }
  13. }

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:

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

4 replies

January 3, 2011

Andre Andre
Robot Herder
6396 posts

Sounds like the same question as this [developer.qt.nokia.com].

Short answer: AFAIK, this is not yet possible*, as the QML model API is private API.

*) in a supported way.

January 3, 2011

YetAnotherNick YetAnotherNi..
Lab Rat
10 posts

Thanks! Seems like I will have to use a QAbstractItemModel and set it in C++. Doesn’t look so pretty either but that’s life.

January 3, 2011

xsacha xsacha
Lab Rat
517 posts

This is something that would be very useful.
The workaround would be too complicated.
For now, do it (i mean the model) in the C++ and then it is much easier to manipulate both in QML and C++.

 Signature 

- Sacha

January 3, 2011

syrianzoro syrianzoro
Lab Rat
63 posts

I have did the same as in this example, I replaced the qlist with standard model but in vain

  1. #include <QAbstractListModel>
  2. #include <QStringList>
  3.  
  4. //![0]
  5. class Animal
  6. {
  7. public:
  8.     Animal(const QString &type, const QString &size);
  9. //![0]
  10.  
  11.     QString type() const;
  12.     QString size() const;
  13.  
  14. private:
  15.     QString m_type;
  16.     QString m_size;
  17. //![1]
  18. };
  19.  
  20. class AnimalModel : public QAbstractListModel
  21. {
  22.     Q_OBJECT
  23. public:
  24.     enum AnimalRoles {
  25.         TypeRole = Qt::UserRole + 1,
  26.         SizeRole
  27.     };
  28.  
  29.     AnimalModel(QObject *parent = 0);
  30. //![1]
  31.  
  32.     void addAnimal(const Animal &animal);
  33.  
  34.     int rowCount(const QModelIndex & parent = QModelIndex()) const;
  35.  
  36.     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
  37.  
  38. private:
  39.     QList<Animal> m_animals;
  40. //![2]
  41. };

 Signature 

Qt is the future

 
  ‹‹ QML Text item + OSX      [SOLVED]Undocumented features ››

You must log in to post a reply. Not a member yet? Register here!