使用Qt显示随机星星的效果

这个示例展示了如何用Qt框架创建一个窗口应用,其中自定义的`MyWidget`类重写了`paintEvent`来绘制背景。`ObjInSky`基类和它的子类`Star`定义了天体对象,`UiPicture`类负责利用`QPainter`绘制这些对象。程序利用`Util`类进行尺寸获取和随机位置生成。
摘要由CSDN通过智能技术生成

Headers:

1.mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

#include "uipicture.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MyWidget; }
QT_END_NAMESPACE

class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget *parent = nullptr);
~MyWidget();

void paintEvent(QPaintEvent *event) override;

UiPicture *uipic;

private:
Ui::MyWidget *ui;
};
#endif // MYWIDGET_H

2.objinsky.h

#ifndef OBJINSKY_H
#define OBJINSKY_H

#include <QString>
#include <QPoint>

class ObjInSky
{
private:
QPoint _pos;
QString _shape;
Qt::GlobalColor _color;
int _size;

public:
ObjInSky(const QPoint &pos, QString shape, Qt::GlobalColor color=Qt::white, int size=20);
virtual ~ObjInSky();


Qt::GlobalColor color() const;
QPoint pos() const;
void setPos(QPoint newPos);
const QString &shape() const;
void setShape(const QString &newShape);
void setColor(Qt::GlobalColor newColor);
int size() const;
void setSize(int newSize);
};

#endif // OBJINSKY_H

3.star.h

#ifndef STAR_H
#define STAR_H

#include "objinsky.h"

class Star : public ObjInSky
{
public:
Star(const QPoint &pos, QString shape="★", Qt::GlobalColor color=Qt::yellow, int size=20);
~Star();
};

#endif // STAR_H

4.uipicture.h

#ifndef UIPICTURE_H
#define UIPICTURE_H

#include <QPainter>
#include "objinsky.h"

class UiPicture
{
private:
QPainter *painter;
QVector<ObjInSky *> objarr;

public:
UiPicture(QPainter *p, int n);
void drawBackground();
void drawObjects();

void drawAll();

virtual ~UiPicture();
void setPainter(QPainter *newPainter);
};

#endif // UIPICTURE_H

5.util.h

#ifndef UTIL_H
#define UTIL_H

#include <QPoint>
#include <QDebug>

class Util
{
static int width;
static int height;

public:
Util()=delete;
static int getWidth();
static int getHeight();
static void setWidth(int newWidth);
static void setHeight(int newHeight);
static int rand();
static QPoint randPos();
};


#endif // UTIL_H

Sources:

1.main.cpp

#include "mywidget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}

2.mywidget.cpp

#include <QDebug>
#include <QPainter>
#include "star.h"
#include "util.h"

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

Util::setHeight(this->height());
Util::setWidth(this->width());

//Star *ps = new Star(QPoint(Util::getWidth(), Util::getHeight()));
Util::setHeight(this->height());
Util::setWidth(this->width());

uipic = new UiPicture(nullptr, 500);


}

MyWidget::~MyWidget()
{
delete uipic;

delete ui;
}


void MyWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

Util::setHeight(this->height());
Util::setWidth(this->width());

QPainter p(this);
uipic->setPainter(&p);
uipic->drawAll();
}

3.objinsky.cpp

#include "objinsky.h"

Qt::GlobalColor ObjInSky::color() const
{
return _color;
}

QPoint ObjInSky::pos() const
{
return _pos;
}

void ObjInSky::setPos(QPoint newPos)
{
_pos = newPos;
}

const QString &ObjInSky::shape() const
{
return _shape;
}

void ObjInSky::setShape(const QString &newShape)
{
_shape = newShape;
}

void ObjInSky::setColor(Qt::GlobalColor newColor)
{
_color = newColor;
}

int ObjInSky::size() const
{
return _size;
}

void ObjInSky::setSize(int newSize)
{
_size = newSize;
}

ObjInSky::ObjInSky(const QPoint &pos, QString shape, Qt::GlobalColor color, int size)
:_pos(pos), _shape(shape), _color(color), _size(size)
{

}

ObjInSky::~ObjInSky()
{

}

4.star.cpp

#include "star.h"

Star::Star(const QPoint &pos, QString shape, Qt::GlobalColor color, int size)
:ObjInSky(pos, shape, color, size)
{

}

Star::~Star()
{

}

5.uipicture.cpp

#include "uipicture.h"
#include "util.h"
#include "star.h"

void UiPicture::setPainter(QPainter *newPainter)
{
painter = newPainter;
}

UiPicture::UiPicture(QPainter *p, int n)
:painter(p), objarr(n)
{
//qDebug() << "uipic contructor";
for (int i = 0; i < n; i++)
{
objarr[i] = new Star(QPoint(Util::randPos()));
}
//qDebug() << "uipic contructor over";
}

void UiPicture::drawBackground()
{
painter->setBrush(Qt::blue);
painter->drawRect(0, 0, Util::getWidth(), Util::getHeight());
}

void UiPicture::drawObjects()
{
//qDebug() << "drawObjects";
for (int i = 0; i < objarr.size(); i++)
{
painter->setPen(objarr[i]->color());
painter->drawText(objarr[i]->pos(), objarr[i]->shape());
}
}

void UiPicture::drawAll()
{
//qDebug() << "drawall";
drawBackground();
drawObjects();
}

UiPicture::~UiPicture()
{
for (int i = 0; i < objarr.size(); i++)
{
delete objarr[i];
}
}

6.util.cpp

#include "util.h"
#include <QRandomGenerator>

int Util::width=0;
int Util::height=0;

int Util::getWidth()
{
return width;
}

int Util::getHeight()
{
return height;
}

void Util::setWidth(int newWidth)
{
width = newWidth;
}

void Util::setHeight(int newHeight)
{
height = newHeight;
}


int Util::rand()
{
return QRandomGenerator::global()->generate();
}


QPoint Util::randPos()
{
return QPoint(rand() % width, rand() % height);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值