#include "renderarea.h"
RenderArea::RenderArea(const QPainterPath &path, QWidget *parent) : QWidget(parent),path(path)
{
}
void RenderArea::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.scale(width()/100.0,height()/100.0);
painter.setBrush(Qt::red);
path.setFillRule(Qt::OddEvenFill);//
// path.setFillRule(Qt::WindingFill);//填充规则
painter.drawPath(path);
}
#include "renderarea.h"
RenderArea::RenderArea(const QPainterPath &path, QWidget *parent) : QWidget(parent),path(path)
{
}
void RenderArea::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.scale(width()/100.0,height()/100.0);
painter.setBrush(Qt::red);
path.setFillRule(Qt::OddEvenFill);//
// path.setFillRule(Qt::WindingFill);//填充规则
painter.drawPath(path);
}
/*----------------------------------------------------------------------------*/
#include "widget.h"
#include "ui_widget.h"
#include <QtWidgets>
#include <QPainter>
#include <QPainterPath>
#include <QGridLayout>
#include <QFont>
#include <math.h>
const float PI = 31.415926f;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QPainterPath rectpath;
rectpath.moveTo(20.0,30.0);
rectpath.lineTo(80.0,30.0);
rectpath.lineTo(80.0,70.0);
rectpath.lineTo(20.0,70.0);
rectpath.closeSubpath();
QPainterPath roundRectPath;
roundRectPath.moveTo(80.0,35.0);
roundRectPath.arcTo(70.0,30.0,10.0,10.0,0.0,90.0);
roundRectPath.lineTo(25.0,30);
roundRectPath.arcTo(20,30,10,10,90,90);
roundRectPath.lineTo(20,65);
roundRectPath.arcTo(20,60,10,10,180,90);
roundRectPath.lineTo(75,70);
roundRectPath.arcTo(70,60,10,10,270,90);
roundRectPath.closeSubpath();
QPainterPath ellipsePath;
ellipsePath.moveTo(80,50);
ellipsePath.arcTo(20,30,60,40,0,360);
QPainterPath piePath;
piePath.moveTo(50,50);
piePath.arcTo(20,30,60,40,60,240);
piePath.closeSubpath();
QPainterPath polygonPath;
polygonPath.moveTo(10,80);
polygonPath.lineTo(20,10);
polygonPath.lineTo(80,30);
polygonPath.closeSubpath();
QPainterPath groupPath;
groupPath.moveTo(60,40);
groupPath.arcTo(20,20,40,40,0,360); //定点 长宽 角度
groupPath.moveTo(40,40);
groupPath.lineTo(40,40);
groupPath.lineTo(40,80);
groupPath.lineTo(80,81);
groupPath.lineTo(80,40);
groupPath.closeSubpath();
QPainterPath textPath;
QFont timesFont("Times",50);
timesFont.setStretch(QFont::ForceOutline);
textPath.addText(10,70,timesFont,"Hello Qt!");
QPainterPath bezierPath;
bezierPath.addRect(20,20,60,60);
bezierPath.moveTo(0,0);
bezierPath.cubicTo(99,0,50,50,99,99);
bezierPath.cubicTo(0,99,59,59,0,0);
QPainterPath starPath;
starPath.moveTo(90,50);
for(int i=1;i<5;i++)
{
starPath.lineTo(50.0 + 40.0 * cos(0.8 * i *3.14),50.0 + 40.0 * sin(0.8 * i *3.14));
}
starPath.closeSubpath();
renderArea[0]=new RenderArea(rectpath);
renderArea[1]=new RenderArea(roundRectPath);
renderArea[2]=new RenderArea(ellipsePath);
renderArea[4]=new RenderArea(piePath);
renderArea[3]=new RenderArea(polygonPath);
renderArea[5]=new RenderArea(groupPath);
renderArea[6]=new RenderArea(textPath);
renderArea[7]=new RenderArea(bezierPath);
renderArea[8]=new RenderArea(starPath);
QGridLayout *mainLayout = new QGridLayout(this);
for(int i =0;i<9;i++)
{
mainLayout->addWidget(renderArea[i],i/3,i%3);
}
setLayout(mainLayout);
setWindowTitle(tr("Painter"));
resize(600,600);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBrush(Qt::white);
painter.fillRect(rect(),Qt::white);
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "renderarea.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *);
private:
Ui::Widget *ui;
RenderArea *renderArea[9];
};
#endif // WIDGET_H
/*--------------------------------------------------------------------------*/
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}