#ifndef PAINTEDWIDGET_H
#define PAINTEDWIDGET_H

#include <QWidget>
#include <QPainter>

QT_BEGIN_NAMESPACE
namespace Ui { class PaintedWidget; }
QT_END_NAMESPACE

class PaintedWidget : public QWidget
{
Q_OBJECT

public:
PaintedWidget(QWidget *parent = nullptr);
~PaintedWidget();
protected:
void paintEvent(QPaintEvent *);
//added new codes
//重写QWidget的paintEvent()函数

private:
Ui::PaintedWidget *ui;
};
#endif // PAINTEDWIDGET_H
#include "paintedwidget.h"
#include "ui_paintedwidget.h"

PaintedWidget::PaintedWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::PaintedWidget)
{
ui->setupUi(this);
resize(800,600);
setWindowTitle(tr("Pain Demo"));
}
void PaintedWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawLine(100,100,400,400);
//draws a line from (100,100) to (400,400)
painter.setPen(Qt::red);
painter.drawRect(10,10,100,400);
//绘制矩形图形
painter.setPen(QPen(Qt::green,5));
painter.setBrush(Qt::blue);
painter.drawEllipse(50,50,400,200);
//绘制椭圆图形
}

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

Qt绘图_qt