Results 1 to 2 of 2

Thread: Using a QML model from a QDeclarativeItem subclass

  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using a QML model from a QDeclarativeItem subclass

    Hi! I'm creating a QDeclarativeItem subclass:

    Qt Code:
    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. };
    To copy to clipboard, switch view to plain text mode 

    I would like to use it in QML like this:

    Qt Code:
    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. }
    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:

    Qt Code:
    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. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using a QML model from a QDeclarativeItem subclass

    I got answered in the Qt forums: Seems like it's not possible yet.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Custom Model and Proxy Model
    By frank100 in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2010, 14:30
  3. Replies: 8
    Last Post: 12th February 2010, 02:41
  4. How to subclass QTableWidgetItem
    By codemonkey in forum Qt Programming
    Replies: 4
    Last Post: 4th October 2009, 11:11
  5. Subclass
    By merry in forum General Programming
    Replies: 2
    Last Post: 1st March 2007, 10:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.