[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:

    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_PROPERTY(QVariantList model READ getModel WRITE setModel NOTIFY modelChanged)
    Q_PROPERTY(QDeclarativeComponent* delegate READ getDelegate WRITE setDelegate NOTIFY delegateChanged)
    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 *);

    void modelChanged();
    void delegateChanged(QDeclarativeComponent *delegate);
    public slots:

    QVariantList m_data;
    QDeclarativeComponent *m_delegate;

    And here is implementation:
    Graph::Graph(QDeclarativeItem *parent) :
    setFlag(QGraphicsItem::ItemHasNoContents, false);
    this->m_delegate = 0;

    void Graph::setModel(QVariantList const &model) {
    this->m_data = model;
    emit this->modelChanged();

    QVariantList Graph::getModel() const {
    return this->m_data;

    void Graph::setDelegate(QDeclarativeComponent *delegate) {
    this->m_delegate = delegate;
    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());
    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());            


    I have this class registered to QML and in my qml file I have following code:
    id: delegate
    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;


    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.


  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.