【Qt绘制小猪】以建造者模式绘制小猪

效果

学以致用,使用设计模式之建造者模式绘制小猪。

【Qt绘制小猪】以建造者模式绘制小猪_第1张图片

代码

接口:申明绘制的步骤
PigBuilder.h

#ifndef PIGBUILDER_H
#define PIGBUILDER_H

#include 
#include 

class PigBuilder : public QObject
{
    Q_OBJECT
public:
    explicit PigBuilder(QObject *parent = nullptr) : QObject(parent)
    {

    }
    virtual ~PigBuilder()
    {

    }
	// 绘制身体
    virtual void drawBody(QPainter &painter) = 0;
    // 绘制胳膊
    virtual void drawEars(QPainter &painter) = 0;
    // 绘制眼睛
    virtual void drawEyes(QPainter &painter) = 0;
    // 绘制鼻子
    virtual void drawNose(QPainter &painter) = 0;
    // 绘制腿
    virtual void drawLegs(QPainter &painter) = 0;
    // 绘制尾巴
    virtual void drawTail(QPainter &painter) = 0;
};

#endif // PIGBUILDER_H

实例化:实例出具体绘制的小猪
SimplePig

#include "pigbuilder.h"


class SimplePig : public PigBuilder
{
    Q_OBJECT
public:
    explicit SimplePig(QObject *parent = nullptr) : PigBuilder(parent){

    }
    virtual ~SimplePig()
    {

    }

    virtual void drawBody(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(247, 204, 148)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(250, 250), 100, 120);
    }
    virtual void drawEars(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(247, 204, 148)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(200, 180), 40, 50);
        painter.drawEllipse(QPointF(300, 180), 40, 50);
    }
    virtual void drawEyes(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(255, 255, 255)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(225, 235), 15, 25);
        painter.drawEllipse(QPointF(275, 235), 15, 25);

        painter.setBrush(QBrush(QColor(0, 0, 0)));
        painter.drawEllipse(QPointF(225, 235), 8, 15);
        painter.drawEllipse(QPointF(275, 235), 8, 15);
    }
    virtual void drawNose(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(255, 185, 185)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(250, 270), 15, 20);
    }
    virtual void drawLegs(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(247, 204, 148)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(200, 320), 20, 40);
        painter.drawEllipse(QPointF(300, 320), 20, 40);
    }
    virtual void drawTail(QPainter &painter)
    {
        painter.setBrush(QBrush(QColor(247, 204, 148)));
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(QPointF(320, 250), 20, 40);
    }
};

建造者:用于创建小猪
PigDirector

class PigDirector : public QObject
{
    Q_OBJECT
public:
    explicit PigDirector(QObject *parent = nullptr) : QObject(parent)
    {
        m_pigBuilder = nullptr;
    }
    virtual ~PigDirector()
    {

    }

    void setBuilder(PigBuilder *pigBuilder)
    {
        m_pigBuilder = pigBuilder;
    }
    void draw(QPainter &painter){
        if (m_pigBuilder != nullptr)
        {
            m_pigBuilder->drawBody(painter);
            m_pigBuilder->drawEars(painter);
            m_pigBuilder->drawEyes(painter);
            m_pigBuilder->drawNose(painter);
            m_pigBuilder->drawLegs(painter);
            m_pigBuilder->drawTail(painter);
        }
    }

private:
    PigBuilder *m_pigBuilder;
};

窗体:用于显示
MainWidget

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        setFixedSize(500, 500);

        // 创建简单的小猪
        PigBuilder *pigBuilder = new SimplePig();
        // 设置小猪的构建方式
        m_pigDirector.setBuilder(pigBuilder);
    }
    ~MainWidget()
    {

    }

protected:
    void paintEvent(QPaintEvent *event) override
    {
        Q_UNUSED(event);

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 构建并绘制小猪
        m_pigDirector.draw(painter);
    }


private:
    PigDirector m_pigDirector;
};

调用:

#include "mainwindow.h"
#include 

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

    return a.exec();
}

你可能感兴趣的:(Qt记录,qt,建造者模式,开发语言)