I want to override QGraphicsPixmapItem that can show an Image and can draw using QPainter on the same item.
I have overridden the class and mouse event and paint methods.When ever i click button it successfully calls mouse and paint events but it does not paint on the image pixmap.
I load image to this item object using setPixmap function i.e.
QGraphicsSelectionPixmapItem selectionitem;
selectionitem->setPixmap(imagePixmap);
This is my overridden class:
Header File: qgraphicsselectionpixmapitem.h
#ifndef QGRAPHICSSELECTIONPIXMAPITEM_H
#define QGRAPHICSSELECTIONPIXMAPITEM_H
#include <QGraphicsPixmapItem>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QList>
#include <QVector>
#include <QPainter>
#include <QPixmap>
class QGraphicsSelectionPixmapItem : public QGraphicsPixmapItem
{
public:
QGraphicsSelectionPixmapItem(QGraphicsItem *parent = NULL);
void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
QList<QPointF> points;
QVector<QPointF> polyPoints;
bool mClickflag;
};
#endif // QGRAPHICSSELECTIONPIXMAPITEM_H
cpp File : qgraphicsselectionpixmapitem.cpp
#include "qgraphicsselectionpixmapitem.h"
QGraphicsSelectionPixmapItem::QGraphicsSelectionPixmapItem(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{
}
void QGraphicsSelectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
mClickflag = true;
qDebug()<<e->pos();
if(e->lastPos()!=e->pos()){
points.append(e->pos());
polyPoints<<e->pos();
update();
}
}
void QGraphicsSelectionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
}
void QGraphicsSelectionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
mClickflag = false;
}
void QGraphicsSelectionPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
qDebug()<<"in Paint ";
if(!polyPoints.empty())
{
painter->setPen(QPen(QColor(255,0,0),3));
painter->drawPolyline(polyPoints);
}
QGraphicsPixmapItem::paint(painter,option,widget);
}