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

效果

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

代码

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

cpp 复制代码
#ifndef PIGBUILDER_H
#define PIGBUILDER_H

#include <QObject>
#include <QPainter>

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

cpp 复制代码
#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

cpp 复制代码
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

cpp 复制代码
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;
};

调用:

cpp 复制代码
#include "mainwindow.h"
#include <QApplication>

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

    return a.exec();
}
相关推荐
好开心334 分钟前
javaScript交互补充(元素的三大系列)
开发语言·前端·javascript·ecmascript
码农多耕地呗27 分钟前
刷别的学校oj—河工大oj1073-1099
开发语言·c++·算法
Envyᥫᩣ30 分钟前
掌握C#中的异步编程:async和await关键字详解
开发语言·c#
sukalot39 分钟前
windows C#-查询表达式基础(三)
开发语言·windows·c#
南东山人1 小时前
一文说清C++类型转换操作符(cast operator)
开发语言·c++
JavaPub-rodert1 小时前
Windows系统下载、安装和配置Python环境变量 --- 《跟着小王学Python》
开发语言·windows·python
Asa121381 小时前
R绘制像素风图片
开发语言·r语言
ooyyaa65611 小时前
sslSocketFactory not supported on JDK 9+
java·开发语言
沐泽Mu2 小时前
嵌入式学习-C嘎嘎-Day04
c语言·开发语言·c++·学习
JAMES费2 小时前
python机器人Agent编程——多Agent框架的底层逻辑(上)
开发语言·python·机器人