February 14, 2012

portoist portoist
Lab Rat
47 posts

[SOLVED] Custom QDeclarativeItem with delegate

 

Hello,
I’v been trying to create own subclass of QDeclarativeItem that would work as simple element that would render plot from given list of points. I’v been successful in rendering given data just by:

  1. painter->drawArc(point.x(),point.y(),5,5,0,5760);

But now I would like to enhance my class a bit. I’ve added delegate property to my class as QDeclarativeComponent and instead of drawing an arc, I would like to draw a delegate. Here is my class header:

  1. class Graph : public QDeclarativeItem
  2. {
  3.     Q_OBJECT
  4.     Q_PROPERTY(QVariantList model READ getModel WRITE setModel NOTIFY modelChanged)
  5.     Q_PROPERTY(QDeclarativeComponent* delegate READ getDelegate WRITE setDelegate NOTIFY delegateChanged)
  6. public:
  7.     explicit Graph(QDeclarativeItem *parent = 0);
  8.    
  9.     void setModel(QVariantList const &m_data);
  10.     QVariantList getModel() const;
  11.  
  12.     void setDelegate(QDeclarativeComponent *delegate);
  13.     QDeclarativeComponent *getDelegate() const;
  14.  
  15.     void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
  16.  
  17. signals:
  18.     void modelChanged();
  19.     void delegateChanged(QDeclarativeComponent *delegate);
  20. public slots:
  21.  
  22. private:
  23.     QVariantList m_data;
  24.     QDeclarativeComponent *m_delegate;
  25. };

And here is implementation:

  1. Graph::Graph(QDeclarativeItem *parent) :
  2.     QDeclarativeItem(parent)
  3. {
  4.     setFlag(QGraphicsItem::ItemHasNoContents, false);
  5.     setCacheMode(QGraphicsItem::DeviceCoordinateCache);
  6.     this->m_delegate = 0;
  7. }
  8.  
  9. void Graph::setModel(QVariantList const &model) {
  10.         this->m_data = model;
  11.         this->update();
  12.         emit this->modelChanged();
  13. }
  14.  
  15. QVariantList Graph::getModel() const {
  16.     return this->m_data;
  17. }
  18.  
  19. void Graph::setDelegate(QDeclarativeComponent *delegate) {
  20.     this->m_delegate = delegate;
  21.     this->update();
  22.     emit this->delegateChanged(delegate);
  23. }
  24.  
  25. QDeclarativeComponent *Graph::getDelegate() const {
  26.     return this->m_delegate;
  27. }
  28.  
  29. void Graph::paint(QPainter *painter, const QStyleOptionGraphicsItem *optionGraphicsItem, QWidget *widget) {
  30.  
  31.     QLine x_axis(0,height(),width(),height());
  32.     QLine y_axis(0,0,0,height());
  33.     painter->drawLine(x_axis);
  34.     painter->drawLine(y_axis);
  35.  
  36.     if (this->m_data.count() > 0 && this->m_delegate != 0){
  37.         QListIterator<QVariant> it(this->m_data);
  38.         while (it.hasNext()){
  39.             QPoint point = it.next().toPoint();
  40.             QObject *object = this->m_delegate->create();
  41.             QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object);
  42.             item->setX((qreal)point.x()); item->setY((qreal)point.y());            
  43.             item->update();            
  44.         }
  45.     }
  46. }

I have this class registered to QML and in my qml file I have following code:

  1.     Component{
  2.  id: delegate
  3.  Rectangle{
  4.      width: 8; height: 8;
  5.      color: "red"
  6.      radius: 4
  7.  }
  8.     }
  9.  
  10.     function data(){
  11.  var list = new Array();
  12.  for (var i=1;i<10;i++){
  13.      var point = new Qt.point(i*10,i*10);
  14.      list[i] = point;
  15.  }
  16.  return list;
  17.     }
  18.  
  19.     Graph{
  20.  width: 600; height: 500
  21.  anchors.centerIn: container
  22.  model: container.data();
  23.  delegate: delegate
  24.     }

But only x and y axis gets rendered and points wont. I’v been trying to call paint method of declarative item in cpp, but it only paints point at 0,0. My question is, how to get delegate objects displayed? Thank you for answers!

2 replies

February 15, 2012

mbrasser mbrasser
Ant Farmer
452 posts

Hi,

The item created by the component needs to be parented (via setParentItem) to another item in the scene (for example, the Graph item) in order to be properly displayed.

Regards,
Michael

February 15, 2012

portoist portoist
Lab Rat
47 posts

Well, that did the trick:) Thanks a lot!
I thought there will be need to set parent, but I was doing it with setParent method and that did nothing.

 
  ‹‹ Using custom C++ models in QML      QML States : Not able to see any transition, rectangle always remains in state1 ››

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