登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

Qt 下 帽式Tab  

2017-06-08 02:57:54|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
先看效果:
Qt 下 帽式Tab - 漠雨 - 沙漠里de烟雨__风尘无名
 
关键代码如下:
#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);
}
  评论这张
 
阅读(358)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018