[Qt-interest] How Can I Paint on QGraphicsView

Prashanth Udupa prashanthudupa at yahoo.com
Mon Aug 17 14:16:35 CEST 2009


You can reimplement mouse event handlers in QGraphicsView to create a QGraphicsPathItem. See code below.

#include <QtGui>

class GraphicsView : public QGraphicsView
{
public:
    GraphicsView() : m_scene(this), m_pathItem(0) {
        m_scene.setSceneRect(-2000, -2000, 4000, 4000);
        setScene(&m_scene);

        setSceneRect(-2000, -2000, 4000, 4000);
    }

    ~GraphicsView() { }

protected:
    void mousePressEvent(QMouseEvent* me) {
        QGraphicsView::mousePressEvent(me);

        if(!me->isAccepted() && !m_pathItem)
        {
            m_pathItem = new QGraphicsPathItem;
            m_scene.addItem(m_pathItem);

            QPainterPath path;
            path.moveTo(mapToScene(me->pos()));
            m_pathItem->setPath(path);
            m_pathItem->setPen(QPen(Qt::blue, 3));
        }
    }

    void mouseMoveEvent(QMouseEvent* me) {
        if(m_pathItem)
        {
            QPainterPath path = m_pathItem->path();
            path.lineTo(mapToScene(me->pos()));
            m_pathItem->setPath(path);
        }
        else
            QGraphicsView::mouseMoveEvent(me);
    }

    void mouseReleaseEvent(QMouseEvent* me) {
        m_pathItem = 0;
        QGraphicsView::mouseReleaseEvent(me);
    }

private:
    QGraphicsScene m_scene;
    QGraphicsPathItem* m_pathItem;
};

int main(int argc, char** argv)
{
    QApplication a(argc, argv);

    GraphicsView gView;
    gView.show();

    return a.exec();
}


HTH.

/ Prashanth



________________________________
From: Sujan Dasmahapatra <sujan.dasmahapatra at gmail.com>
To: qt-interest at trolltech.com
Sent: Monday, August 17, 2009 2:27:33 PM
Subject: [Qt-interest] How Can I Paint on QGraphicsView


Dear Friends
How can I draw on the QGraphicsView using a QPainter, I want to draw continuously on the view when user Press mouse till Release mouse.
I tried to implement paintEvent() method but that can be used for widget painting and not QGraphicsView.
Any other means are there to paint on the QGraphicsView ?? Please help me 

-- 
Thanks & Regards
S. Dasmahapatra
B.E. (Aeronautics-Aerodynamics)
Bangalore, India
Ph:91-9900839788
Office:91-80-66470248
mail id : sujan.dasmahapatra at gmail.com

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090817/1b4cceb1/attachment.html 


More information about the Qt-interest-old mailing list