Qt绘图
精选
原创
©著作权归作者所有:来自51CTO博客作者LazyUpdate的原创作品,请联系作者获取转载授权,否则将追究法律责任
#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;
}