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?

    thanks

    @
    #include <QDeclarativeItem>
    #include <QtGui>

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

    private:
    QGraphicsScene* _scene;
    QGraphicsView* _view;

    signals:
    public slots:

    };

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

    _scene = new QGraphicsScene();
    
    _scene->setBackgroundBrush(Qt::blue);
    
    _view = new QGraphicsView();
    _view->setScene(_scene);
    
    QGraphicsRectItem ri(0, 0, 50, 50, this, scene());
    ri.setBrush(QBrush(Qt::green));
    

    }
    @



  • 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
 

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