[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(QVariantList model READ getModel WRITE setModel NOTIFY modelChanged)
Q_PROPERTY(QDeclarativeComponent* delegate READ getDelegate WRITE setDelegate NOTIFY delegateChanged)
public:
explicit Graph(QDeclarativeItem *parent = 0);void setModel(QVariantList const &m_data); QVariantList getModel() const; void setDelegate(QDeclarativeComponent *delegate); QDeclarativeComponent *getDelegate() const; void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
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)
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
setCacheMode(QGraphicsItem::DeviceCoordinateCache);
this->m_delegate = 0;
}void Graph::setModel(QVariantList const &model) {
this->m_data = model;
this->update();
emit this->modelChanged();
}QVariantList Graph::getModel() const {
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) {
QLine x_axis(0,height(),width(),height()); QLine y_axis(0,0,0,height()); painter->drawLine(x_axis); painter->drawLine(y_axis); if (this->m_data.count() > 0 && this->m_delegate != 0){ QListIterator<QVariant> it(this->m_data); while (it.hasNext()){ QPoint point = it.next().toPoint(); QObject *object = this->m_delegate->create(); 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++){
var point = new Qt.point(i10,i10);
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!
-
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 -
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.