Painting a dynamically created QQuickPaintedItem subclass



  • Hi everyone. I'd like to know why I can't paint a dynamically created QQuickPaintedItem subclass. Here is an example :

    @
    // the class I need to draw
    class Circle : public QQuickPaintedItem
    {
    Q_OBJECT
    // ... some convenient properties along with their setters and getters
    public:
    Circle(QQuickItem *parent = 0) : QQuickPaintedItem(parent) { }
    void Circle::paint(QPainter *painter) {
    //... set a QPen and draw
    painter->drawEllipse(boudingRect(). adjusted(1, 1, -1, -1));
    }
    }
    @

    @
    // The circle class is registered in main.cpp
    qmlRegisterType<Circle>("Stuff", 1, 0, "Circle");
    @

    -> I can see the circle if I use the Circle in a qml file.
    @
    Circle {
    // this circle is drawn
    }
    @

    // Now, imagine that I need a container, we'll call it "Chart". This
    container is also registered.
    @
    class Chart : public QQuickItem {
    Q_OBJECT
    // ... some convenient properties along with their setters and getters

    public:
    Chart(QQuickItem *parent = 0) : QQuickItem(parent) {
    m_circle = new Circle(this); // should be the same as setParentItem(this)
    m_circle->setContentsSize(QSize(width(), height())); // it needs a size
    m_circle->setParent(this); // different from parent item
    // setFlag(ItemHasContent, true|flase) has no effect
    }
    Circle *m_circle;
    }
    @

    You can see that the Circle is dynamically allocated. It is not drawn, I
    don't know why. The Circle::paint method is not called. It is as if the
    scene does not know about it.
    At first, I thought it was because of the chart being only a QQuickItem and not a painted item. (This is why I played with the ItemHasContent flag )
    so, I've put another circle inside the chart in QML :

    @
    Chart {
    ...
    Circle {
    // this circle is drawn but there should be also another circle (the one created in C++)
    }
    }
    @



  • Today , when reading "this blog":http://www.kdab.com/analysing-qtquick-apps-gammaray-button-gone/?utm_source=rss&utm_medium=rss&utm_campaign=analysing-qtquick-apps-gammaray-button-gone post from yesterday, I stumbled on

    bq. Might it be size ‘zero’ or misplaced?

    I tried to give a size to the circle, and that's it, it's drawn.


Log in to reply
 

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