零基础开始QT绘图(5)

版权声明:本博客所有原创文章未经准许不得转载或保存转发,本人保留版权法律追诉权。 https://blog.csdn.net/haigear/article/details/85245846

上一篇,我们学会了如何在GraphicView上绘制GraphicItem,看起来效果和在Painter上绘制的没什么差别,今天我一起来看看他们的差别。我们在上一篇代码的基础上加上几句简单的设置,看看效果。

一、让图形动起来

#include "mywidget.h"
#include "ui_mywidget.h"

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

    scene=new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    QGraphicsRectItem * rect=new QGraphicsRectItem(QRect(20,20,this->width()/2,this->height()/2));
    QGraphicsTextItem *item = new QGraphicsTextItem("GraphicView running here!");
    //设置矩形可以被移动和被选择
    rect->setFlag(QGraphicsItem::ItemIsMovable);
    rect->setFlag(QGraphicsItem::ItemIsSelectable);
   
       scene->addItem(rect);
       scene->addItem(item);
}

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

没有加SetFlag前的运行效果如下:
在这里插入图片描述
增加之后,很明显这个矩形是可以被鼠标选中,成虚线状态,而且可以被移动,当移动的大于边界线就会出现滚动条:
在这里插入图片描述
当然,这些都是固化了的GraphicItem。其实我们在编写游戏或者组态软件的时候更多的是需要有个性的GraphicItem,那么下面我们就来自己编写一个自定义的GraphicItem,来实现我们个性化的“梦想”。

二、定义自己的GraphicItem
首先我们在QT中添加一个类,让它继承自GraphicItem,代码如下:
头文件

#ifndef CUSTOMGRAPHICITEM_H
#define CUSTOMGRAPHICITEM_H
#include <QPainter>
#include <QGraphicsItem>
  
class CustomGraphicItem:public QGraphicsItem
{
public:
    CustomGraphicItem();
    QRectF boundingRect( ) const;
    void paint(QPainter *painter,
              const QStyleOptionGraphicsItem *option,
              QWidget *widget);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

};

#endif // CUSTOMGRAPHICITEM_H

cpp文件:

#include "customgraphicitem.h"

CustomGraphicItem::CustomGraphicItem()
{
    QGraphicsItem::setFlag(QGraphicsItem::ItemIsMovable);
}

QRectF CustomGraphicItem::boundingRect() const
{
    return QRectF(-10, -10, 120, 40);
}

void CustomGraphicItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF recf=boundingRect();
    QPen pen(Qt::red);
    QBrush brush(Qt::green);
    painter->setPen(pen);
    painter->setBrush(brush);
    painter->drawRoundedRect(recf,5,5);
}

void CustomGraphicItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
}

void CustomGraphicItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
}

void CustomGraphicItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
}

从代码中我们可以发现,我们仅仅做了几个必要的动作,而且很简单,那就是实现了几个GraphicItem的虚函数,
boundingRect:必须实现的,它决定了图形元素的边界;
paint:必须实现的,它决定了图形长什么模样
mouseMoveEvent:可以选择不实现,如果你想在拖动的时候有什么事件完成就必须实现;
mousePressEvent:可以选择不实现,如果你在鼠标按下的时候有什么事件完成就必须实现;
mouseReleaseEvent:可以选择不实现,如果你在鼠标放开的时候有什么事件完成就必须实现;
补充一点,setFlag在自定义类的构造函数中实现与在外部设置一样,只是在内部实现省去了外部再设置。
我们来看看运行效果:
在这里插入图片描述
这样,我们定义的绿色的倒角矩形就可以像前面我们调用的线程的GraphICRectItem一样拖动了。
好了,这篇就介绍到这里,如果我们想要使自定义的图形元素有更多的花样,我们就要在刚才实现的几个虚函数中增加更多的内容了。精彩在后面,我们下一篇继续介绍。

猜你喜欢

转载自blog.csdn.net/haigear/article/details/85245846
今日推荐