0

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);
}
Saurabh Gohil
  • 75
  • 1
  • 1
  • 9

1 Answers1

0

I found this solution: Use QPainterPath to store and draw anything on QGraphicsItem.

in header file add:

QPainterPath m_path;
bool firstClick;

in cpp file modify mousePressEvent and paint functions:

QGraphicsSelectionPixmapItem::QGraphicsSelectionPixmapItem(QGraphicsItem *parent): QGraphicsPixmapItem(parent)
{
    firstClick=true;
}

void QGraphicsSelectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    m_path.setFillRule(Qt::WindingFill);
    if(firstClick)
    {
        m_path.moveTo(event->pos());
        firstClick=false;
    }
    else
        m_path.lineTo(event->pos());
    update();
}

void QGraphicsSelectionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    update();
}

void QGraphicsSelectionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{

}

void QGraphicsSelectionPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QGraphicsPixmapItem::paint(painter,option,widget);
    if(m_path.isEmpty())
        return;

    QPen pen(QColor(255,0,0));
    pen.setWidth(1);
    painter->setPen(pen);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setBrush(QColor(255,0,0,100));
    painter->drawPath(m_path);
}
Saurabh Gohil
  • 75
  • 1
  • 1
  • 9