• 1403阅读
  • 4回复

[讨论]重绘两边半圆中间矩形的疑问 [复制链接]

上一主题 下一主题
离线lwei24
 

只看楼主 倒序阅读 楼主  发表于: 2022-05-16
如题,在项目开发中,自己想重绘一个两边半圆中间矩形的按钮,具体代码如下:
  1. QPainterPath CWidget::makeSemicircleEdgeRectanglePath(QRect rect)
  2. {
  3.     int x = rect.x();
  4.     int y = rect.y();
  5.     int w = rect.width();
  6.     int h = rect.height();
  7.     int r = h/2;
  8.     QPoint leftCenter(x+r,y+r);            
  9.     QPoint rightCenter(x+w-r,y+r);          
  10.     QPoint leftTop(x+r,y);                  
  11.     QPoint rightBottom(x+w-r,y+h);          
  12.     QRect leftSemicircleRect(x,y,h,h);      
  13.     QRect rightSemicircleRect(x+w-h,y,h,h);
  14.     QPainterPath semicircleEdgeRectanglePath;
  15.     semicircleEdgeRectanglePath.moveTo(leftCenter);
  16.     semicircleEdgeRectanglePath.arcTo(leftSemicircleRect,90,180);
  17.     semicircleEdgeRectanglePath.lineTo(rightBottom);
  18.     semicircleEdgeRectanglePath.arcTo(rightSemicircleRect,270,180);
  19.     semicircleEdgeRectanglePath.lineTo(leftTop);
  20.     semicircleEdgeRectanglePath.closeSubpath();
  21.     return semicircleEdgeRectanglePath;
  22. }
  23. void CWidget::paintEvent(QPaintEvent *event)
  24. {
  25.     int x = 0, y = 0, w = 120, h = 30;
  26.     QRect rect(x,y,w,h);
  27.     painter.setPen(Qt::NoPen);
  28.     painter.setBrush(QColor(qRgb(20, 28, 43)));
  29.     QPainterPath linePath = this->makeSemicircleEdgeRectanglePath(rect);
  30.     painter.drawPath(linePath);
  31.     QPoint hLPoint(QPoint(rect.x() + rect.height()/2, rect.y()));
  32.     QPoint hRPoint(QPoint(rect.x() + rect.width() + rect.y()/2 - rect.height()/2, rect.y()));
  33.     QPoint uLPoint(rect.x() + rect.height()/2, rect.y() + rect.height()/2 + rect.height()/2);
  34.     QPoint uRPoint(rect.x() + rect.width() - rect.height()/2, rect.y() + rect.height()/2 + rect.height()/2);
  35.     QPen newPen;
  36.     newPen.setColor(Qt::white);
  37.     painter.setPen(newPen);
  38.     painter.setBrush(QColor(qRgb(255, 255, 255)));
  39.     painter.drawLine(hLPoint, hRPoint);
  40.     painter.drawLine(uLPoint, uRPoint);
  41.     QPen pen(Qt::white);
  42.     painter.setPen(pen);
  43.     QFont font;
  44.     font.setFamily("Microsoft Yahei");
  45.     font.setWeight(65);
  46.     painter.setFont(font);
  47.     QTextOption option;
  48.     option.setAlignment(Qt::AlignCenter);
  49.     painter.drawText(rect, "设置", option);
  50. }


以上已实现重绘两边半圆中间矩形,并可以在这个图形里重绘文字,想要在在两边半圆和中间矩形的那两条边都画上颜色,即如代码里,我已将矩形的上下两边都画成白色了,有什么办法或什么接口能将两边半圆的边框也画成白色吗?并且能不能给的矩形上下两条线和两边半圆的圆弧画的线加粗呢?

各位大佬,劳烦帮忙看看,给点意见,小弟在此多谢了!



离线tanyue.esec

只看该作者 1楼 发表于: 2022-05-17
用CSS很方便的吧。


半圆可以用border-radius来实现,border-radius: 15px,设置radius的像素为高度的一半就可以实现半圆弧
边框也可以用border属性,border: 2px solid white,2像素的白色实线,border也可以单独设置四个边,border-left,border-right等,左右加粗就可以单独设置左右的像素。

供参考
1条评分贡献值+1
lwei24 贡献值 +1 - 2022-05-17
离线lwei24

只看该作者 2楼 发表于: 2022-05-17
回 tanyue.esec 的帖子
tanyue.esec:用CSS很方便的吧。
半圆可以用border-radius来实现,border-radius: 15px,设置radius的像素为高度的一半就可以实现半圆弧
边框也可以用border属性,border: 2px solid white,2像素的白色实线,border也可以单独设置四个边,border-left,border-right等,左右加粗就可以单独设 .. (2022-05-17 08:37) 

多谢,这也算是一种办法!
离线liudianwu

只看该作者 3楼 发表于: 2022-05-17
用QPainterPath 两个圆加一个矩形,相切就好。
1条评分贡献值+1
lwei24 贡献值 +1 - 2022-05-17
欢迎关注微信公众号:Qt实战/Qt入门和进阶(各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发) QQ:517216493  WX:feiyangqingyun  QQ群:751439350
离线lwei24

只看该作者 4楼 发表于: 2022-05-17
回 liudianwu 的帖子
liudianwu:用QPainterPath 两个圆加一个矩形,相切就好。 (2022-05-17 09:45) 

哦哦,谢谢啦!
快速回复
限100 字节
 
上一个 下一个