[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:
- 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:
- class Graph : public QDeclarativeItem
- {
- Q_OBJECT
- Q_PROPERTY(QDeclarativeComponent* delegate READ getDelegate WRITE setDelegate NOTIFY delegateChanged)
- public:
- explicit Graph(QDeclarativeItem *parent = 0);
- void setDelegate(QDeclarativeComponent *delegate);
- QDeclarativeComponent *getDelegate() const;
- signals:
- void modelChanged();
- void delegateChanged(QDeclarativeComponent *delegate);
- public slots:
- private:
- QVariantList m_data;
- QDeclarativeComponent *m_delegate;
- };
And here is implementation:
- Graph::Graph(QDeclarativeItem *parent) :
- QDeclarativeItem(parent)
- {
- this->m_delegate = 0;
- }
- this->m_data = model;
- this->update();
- emit this->modelChanged();
- }
- return this->m_data;
- }
- void Graph::setDelegate(QDeclarativeComponent *delegate) {
- this->m_delegate = delegate;
- this->update();
- emit this->delegateChanged(delegate);
- }
- QDeclarativeComponent *Graph::getDelegate() const {
- return this->m_delegate;
- }
- void Graph::paint(QPainter *painter, const QStyleOptionGraphicsItem *optionGraphicsItem, QWidget *widget) {
- painter->drawLine(x_axis);
- painter->drawLine(y_axis);
- if (this->m_data.count() > 0 && this->m_delegate != 0){
- while (it.hasNext()){
- QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object);
- item->setX((qreal)point.x()); item->setY((qreal)point.y());
- item->update();
- }
- }
- }
I have this class registered to QML and in my qml file I have following code:
- Component{
- id: delegate
- Rectangle{
- width: 8; height: 8;
- color: "red"
- radius: 4
- }
- }
- function data(){
- var list = new Array();
- for (var i=1;i<10;i++){
- list[i] = point;
- }
- return list;
- }
- Graph{
- width: 600; height: 500
- anchors.centerIn: container
- model: container.data();
- delegate: delegate
- }
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
You must log in to post a reply. Not a member yet? Register here!


