【问题标题】:Qt :: QGraphicsPixmapItem : overriding paint() method?Qt :: QGraphicsPixmapItem : 覆盖paint() 方法?
【发布时间】:2014-02-19 23:27:38
【问题描述】:

我想覆盖 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);
}

【问题讨论】:

  • 您在先前绘制的多段线上绘制图像。尝试在绘制折线之前调用 QGraphicsPixmapItem::paint()
  • 我试过了,但没有奏效。但我终于找到了解决方案。看我的回答。

标签: qt paint overriding qgraphicsview qgraphicsitem


【解决方案1】:

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

在头文件中添加:

QPainterPath m_path;
bool firstClick;

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

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

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2015-06-21
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多