#include "widget.h"
#include <QPaintEvent>
#include <QPainter>
Widget::Widget(QWidget *parent) : QWidget(parent)
{
this->resize(960,600);
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(Qt::red);
QRect rect0(50,50,150,150);
//先来看看圆角画法;
painter.drawRect(rect0);
painter.drawArc(rect0,0,90*16); //第一象限;
painter.setPen(Qt::green);
painter.drawArc(rect0,90*16,90*16); //第二象限;
painter.setPen(Qt::blue);
painter.drawArc(rect0,180*16,90*16); //第三象限;
painter.setPen(Qt::black);
painter.drawArc(rect0,270*16,90*16); //第四象限;
//再来看看画帽形Tab;
QRect rect1(250,50,300,300);
painter.setPen(Qt::red);
painter.drawRect(rect1);
painter.setPen(Qt::blue);
int W = rect1.width();
int H = rect1.height();
int top_gap = 15;
int top_radius = 30;
int bottom_gap = 30; //==bottom_radius;
int k = 16;
int dVal = 90*k;
int x1 =0, y1 = 0;
int x0 = rect1.x()-bottom_gap;
int y0 = rect1.y()+H-2*bottom_gap;
painter.drawArc(x0,y0,2*bottom_gap,2*bottom_gap,270*k,dVal);
x0 += 2*bottom_gap;
y0 += bottom_gap;
y1 = rect1.y()+top_gap+top_radius;
painter.drawLine(x0,y0,x0,y1);
y0 = rect1.y()+top_gap;
painter.drawArc(x0,y0,2*top_radius,2*top_radius,90*k,dVal);
x0 += top_radius;
x1 = rect1.x()+W-top_radius-bottom_gap;
painter.drawLine(x0,y0,x1,y0);
x0 = x1-top_radius;
painter.drawArc(x0,y0,2*top_radius,2*top_radius,0,dVal);
x0 = x1+top_radius;
y0 += top_radius;
y1 = rect1.y()+H-bottom_gap;
painter.drawLine(x0,y0,x0,y1);
y0 = y1-bottom_gap;
painter.drawArc(x0,y0,2*bottom_gap,2*bottom_gap,180*k,dVal);
//最后来看看封闭曲线与填充;
QRect rect2(650,50,300,300);
painter.setPen(Qt::red);
painter.drawRect(rect2);
painter.setPen(Qt::blue);
painter.setBrush(QBrush(Qt::gray));
QPainterPath path;
////从左侧画起;
int x2 = rect2.x();
int y2 = rect2.y()+rect2.height();
path.moveTo(x2,y2);
x2 = rect2.x()-bottom_gap;
y2 = rect2.y()+rect2.height()-2*bottom_gap;
path.arcTo(x2,y2,2*bottom_gap,2*bottom_gap,270,90); //270指当前所在角度,90指逆时针转90度,若为负,则为顺时针;
x2 += 2*bottom_gap;
y2 = rect2.y()+top_gap+top_radius;
path.lineTo(x2,y2);
y2 = rect2.y()+top_gap;
path.arcTo(x2,y2,2*top_radius,2*top_radius,180,-90);
x2 += top_radius;
// path.moveTo(x2,y2);
x2 = rect2.x()+rect2.width()-bottom_gap-top_radius;
path.lineTo(x2,y2);
x2 -= top_radius;
path.arcTo(x2,y2,2*top_radius,2*top_radius,90,-90);
x2 += 2*top_radius;
y2 += top_radius;
// path.moveTo(x2,y2);
y2 = rect2.y()+rect2.height()-bottom_gap;
path.lineTo(x2,y2);
y2 -= bottom_gap;
path.arcTo(x2,y2,2*bottom_gap,2*bottom_gap,180,90);
// path.moveTo(rect2.x()+rect2.width(),rect2.y()+rect2.height());
path.lineTo(rect2.x(),rect2.y()+rect2.height());
////或从右侧画起;
// int x2 = rect2.x()+rect2.width();
// int y2 = rect2.y()+rect2.height();
// path.moveTo(x2,y2);
// x2 -= bottom_gap;
// y2 -= 2*bottom_gap;
// path.arcTo(x2,y2,2*bottom_gap,2*bottom_gap,270,-90);
// y2 += bottom_gap;
// // path.moveTo(x2,y2);
// y2 = rect2.y()+top_gap+top_radius;
// path.lineTo(x2,y2);
// x2 -= 2*top_radius;
// y2 -= top_radius;
// path.arcTo(x2,y2,2*top_radius,2*top_radius,0,90);
// x2 += top_radius;
//// path.moveTo(x2,y2);
// x2 = rect2.x()+bottom_gap+top_radius;
// path.lineTo(x2,y2);
// x2 -= top_radius;
// path.arcTo(x2,y2,2*top_radius,2*top_radius,90,90);
// y2 += top_radius;
//// path.moveTo(x2,y2);
// y2 = rect2.y()+rect2.height()-bottom_gap;
// path.lineTo(x2,y2);
// x2 -= 2* bottom_gap;
// y2 -= bottom_gap;
// path.arcTo(x2,y2,2*bottom_gap,2*bottom_gap,0,-90);
// path.lineTo(rect2.x()+rect2.width(),rect2.y()+rect2.height());
painter.drawPath(path);
QWidget::paintEvent(event);
}
评论