#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void paintEvent(QPaintEvent *event) override
{
/* 实例化画家对象 this指定的是绘图设备 */
QPainter painter(this);
/* 设置画笔 */
QPen pen(QColor(255, 0, 0));
pen.setWidth(2);
pen.setStyle(Qt::DotLine); //设置画笔风格
painter.setPen(pen); //让画家使用这个笔
/* 设置画刷 */
QBrush brush(Qt::green);
brush.setStyle(Qt::Dense2Pattern); //设置画刷风格
painter.setBrush(brush); //让画家使用画刷
/* 画线 */
painter.drawLine(QPoint(0, 0), QPoint(100, 100));
/* 画圆 */
painter.drawEllipse(QPoint(100, 100), 50, 50);
/* 画矩形 */
painter.drawRect(QRect(150, 150, 50, 50));
/* 画文字 */
painter.drawText(QPoint(0, 250), "hello furong");
}
private:
Ui::Widget *ui;
};
#endif // WIDGET_H