Important: Please read the Qt Code of Conduct -

Painting QGraphicsScene inside custom QDeclarativeItem

  • Hi everybody,

    i am trying to implement a drawing/painting application, so i expect to be able of adding predefined backgrounds, texts, shapes and images. To this, i have a WorkspaceItem class (inherited from QDeclcarativeItem).

    WorkspaceItem has a QGraphicsScene and a QGraphicsView, but when running my app, the scene is not painted.

    I am also trying to add a QGraphicsRectItem directly to the WorkspaceItem but also it is not painted :S

    How can i do this?


    #include <QDeclarativeItem>
    #include <QtGui>

    class WorkspaceItem : public QDeclarativeItem
    explicit WorkspaceItem(QDeclarativeItem *parent = 0);

    QGraphicsScene* _scene;
    QGraphicsView* _view;

    public slots:


    WorkspaceItem::WorkspaceItem(QDeclarativeItem *parent) : QDeclarativeItem(parent){
    setFlag(QGraphicsItem::ItemHasNoContents, false);

    _scene = new QGraphicsScene();
    _view = new QGraphicsView();
    QGraphicsRectItem ri(0, 0, 50, 50, this, scene());


  • I think you forgot to overload the paint method:
    QDeclarativeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

    You can then use the painter to draw your content.

Log in to reply