Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

Painting a dynamically created QQuickPaintedItem subclass

$
0
0
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++)     } }

Viewing all articles
Browse latest Browse all 4972

Trending Articles