Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Custom QDeclarativeItem with delegate
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Custom QDeclarativeItem with delegate

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    portoist
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        portoist
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved