Qt :: QGraphicsPixmapItem:重写paint()方法?

时间:2014-02-17 19:36:10

标签: qt paint override qgraphicsview qgraphicsitem

我想覆盖可以显示图像的QGraphicsPixmapItem,并且可以在同一个项目上使用QPainter进行绘制。

我已经覆盖了类和鼠标事件以及绘制方法。当我单击按钮时,它成功调用鼠标和绘制事件,但它不会在图像像素图上绘制。

我使用setPixmap函数将图像加载到此项目对象 即。

QGraphicsSelectionPixmapItem selectionitem;
selectionitem->setPixmap(imagePixmap); 

这是我被覆盖的课程:

头文件: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文件: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);
}

1 个答案:

答案 0 :(得分:0)

我找到了这个解决方案: 使用QPainterPath在QGraphicsItem上存储和绘制任何内容。

在头文件中添加:

QPainterPath m_path;
bool firstClick;

在cpp文件中修改mousePressEvent和绘制函数:

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);
}