QPaintPath

#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();
}

 

转载于:https://my.oschina.net/osyunwei/blog/718438

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值