[Solved] Is it possible? I want to paint on QGraphicsScene !!HELP..



  • to my paint application need to insert mousepress,release and move events to draw on graphics scene..and other functionalists i want it same as "Diagram Scene"..how can i approch to this.


  • Moderators

    Hi,

    The best option would be to subclass QGraphicsScene and reimplementing its mousePressEvent, mouseMoveEvent and mouseReleaseEvent.
    For Drawing you can use "QGraphicsPathItem":http://qt-project.org/doc/qt-4.8/qgraphicspathitem.html and then "setPath":http://qt-project.org/doc/qt-4.8/qgraphicspathitem.html#setPath to "QPainterPath":http://qt-project.org/doc/qt-4.8/qpainterpath.html

    Here'a an example..
    @
    #include "paintscene.h"

    PaintScene::PaintScene(QObject *parent) :
    QGraphicsScene(parent)
    {
    pressed = false;
    }

    void PaintScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    m_item = new QGraphicsPathItem();
    QPainterPath path;
    path.moveTo(event->scenePos());
    m_item->setPath(path);
    addItem(m_item);
    pressed = true;
    }

    void PaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
    if(pressed)
    {
    QPainterPath path = m_item->path();
    path.lineTo(event->scenePos());
    m_item->setPath(path);
    }
    }

    void PaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
    pressed = false;
    m_item = NULL;
    }
    @
    --- paintscene.h

    @
    #ifndef PAINTSCENE_H
    #define PAINTSCENE_H

    #include <QGraphicsScene>
    #include <QGraphicsPathItem>
    #include <QGraphicsSceneMouseEvent>

    class PaintScene : public QGraphicsScene
    {
    Q_OBJECT
    public:
    explicit PaintScene(QObject *parent = 0);

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

    signals:

    public slots:

    private:
    QGraphicsPathItem *m_item;
    QPointF previous;
    bool pressed;

    };

    #endif // PAINTSCENE_H
    @

    Hope this helps a bit..



  • hey thanku :)


Log in to reply
 

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