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. Painting a dynamically created QQuickPaintedItem subclass
QtWS25 Last Chance

Painting a dynamically created QQuickPaintedItem subclass

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.0k Views
  • 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.
  • _ Offline
    _ Offline
    _Michel
    wrote on last edited by
    #1

    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++)
    }
    }
    @

    1 Reply Last reply
    0
    • _ Offline
      _ Offline
      _Michel
      wrote on last edited by
      #2

      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.

      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