Qt文档阅读笔记-QGraphicsEffect::draw(QPainter *painter)官方解析与实例

目录

官方解析

博主例子


 

官方解析

QGraphicsEffect::draw(QPainter *painter)
这个纯虚函用于绘制效果,并且在原图需要被绘制的时候被调用。
在QGraphicsEffect的派生类里面重写这个函数是用于提供使用painter达到绘制的效果。
栗子:

  MyGraphicsEffect::draw(QPainter *painter)
  {
      ...
      QPoint offset;
      if (sourceIsPixmap()) {
          // No point in drawing in device coordinates (pixmap will be scaled anyways).
          const QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
          ...
          painter->drawPixmap(offset, pixmap);
      } else {
          // Draw pixmap in device coordinates to avoid pixmap scaling;
          const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset);
          painter->setWorldTransform(QTransform());
          ...
          painter->drawPixmap(offset, pixmap);
      }
      ...
  }


这个函数不应该被用户显示的调用,因为这个函数只用于重新实现。

博主例子

自己写的很有趣的小例子,程序运行截图如下:

源码如下:

blureffect.h

#ifndef BLUREFFECT_H
#define BLUREFFECT_H

#include 
#include 

QT_BEGIN_NAMESPACE
class QGraphicsItem;
QT_END_NAMESPACE

class BlurEffect : public QGraphicsBlurEffect
{
public:
    BlurEffect(QGraphicsItem *item);
    void setBaseLine(const qreal y);
    void setGraphicsItemSize(const QSize size);
    void draw(QPainter *painter)Q_DECL_OVERRIDE;

private:
    void adjustForItem();
    qreal m_baseLine;
    QGraphicsItem *m_item;
    QSize m_size;
};

#endif // BLUREFFECT_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

class BlurEffect;

QT_BEGIN_NAMESPACE
class QGraphicsPixmapItem;
QT_END_NAMESPACE

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

public slots:
    void timeout();

private:
    Ui::Widget *ui;
    BlurEffect *m_blurEffect;
    QGraphicsPixmapItem *m_item;
};

#endif // WIDGET_H

blureffect.cpp

#include "blureffect.h"
#include 
#include 
#include 
#include 

BlurEffect::BlurEffect(QGraphicsItem *item):
    QGraphicsBlurEffect(),
    m_baseLine(200),
    m_item(item)
{

}

void BlurEffect::setBaseLine(const qreal y)
{
    m_baseLine=y;
}

void BlurEffect::setGraphicsItemSize(const QSize size)
{
    m_size=size;
}

void BlurEffect::draw(QPainter *painter)
{
    adjustForItem();
    painter->save();
    QPen pen(QColor(abs((int)m_baseLine%256),abs((int)m_baseLine%256),abs((int)m_baseLine%256)));
    pen.setWidth(3);
    painter->setPen(pen);
    painter->drawRect(0,0,m_size.width(),m_size.height());
    painter->restore();

    QGraphicsBlurEffect::draw(painter);
}

void BlurEffect::adjustForItem()
{
    qreal y=m_baseLine-m_item->scenePos().y();
    qreal radius=qBound(qreal(0.0),y/32,qreal(16.0));
    setBlurRadius(radius);
}

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "blureffect.h"
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("CSDN IT1995");
    ui->graphicsView->setScene(new QGraphicsScene);
    m_item=new QGraphicsPixmapItem;
    m_item->setPixmap(QPixmap(":/src.jpg"));

    ui->graphicsView->scene()->addItem(m_item);
    m_item->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable);

    m_blurEffect=new BlurEffect(m_item);
    m_blurEffect->setGraphicsItemSize(m_item->boundingRect().size().toSize());
    m_item->setGraphicsEffect(m_blurEffect);

    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    timer->start(500);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timeout()
{
    m_blurEffect->setBaseLine(m_item->scenePos().x()+m_item->scenePos().y());
}

 

你可能感兴趣的:(C/C++,Qt,文档阅读笔记)