lwei24 |
2022-05-16 14:46 |
重绘两边半圆中间矩形的疑问
如题,在项目开发中,自己想重绘一个两边半圆中间矩形的按钮,具体代码如下:
- QPainterPath CWidget::makeSemicircleEdgeRectanglePath(QRect rect)
{ int x = rect.x(); int y = rect.y(); int w = rect.width(); int h = rect.height(); int r = h/2; QPoint leftCenter(x+r,y+r); QPoint rightCenter(x+w-r,y+r); QPoint leftTop(x+r,y); QPoint rightBottom(x+w-r,y+h); QRect leftSemicircleRect(x,y,h,h); QRect rightSemicircleRect(x+w-h,y,h,h);
QPainterPath semicircleEdgeRectanglePath; semicircleEdgeRectanglePath.moveTo(leftCenter); semicircleEdgeRectanglePath.arcTo(leftSemicircleRect,90,180); semicircleEdgeRectanglePath.lineTo(rightBottom); semicircleEdgeRectanglePath.arcTo(rightSemicircleRect,270,180); semicircleEdgeRectanglePath.lineTo(leftTop); semicircleEdgeRectanglePath.closeSubpath();
return semicircleEdgeRectanglePath; }
void CWidget::paintEvent(QPaintEvent *event) { int x = 0, y = 0, w = 120, h = 30; QRect rect(x,y,w,h); painter.setPen(Qt::NoPen); painter.setBrush(QColor(qRgb(20, 28, 43))); QPainterPath linePath = this->makeSemicircleEdgeRectanglePath(rect); painter.drawPath(linePath); QPoint hLPoint(QPoint(rect.x() + rect.height()/2, rect.y())); QPoint hRPoint(QPoint(rect.x() + rect.width() + rect.y()/2 - rect.height()/2, rect.y())); QPoint uLPoint(rect.x() + rect.height()/2, rect.y() + rect.height()/2 + rect.height()/2); QPoint uRPoint(rect.x() + rect.width() - rect.height()/2, rect.y() + rect.height()/2 + rect.height()/2);
QPen newPen; newPen.setColor(Qt::white); painter.setPen(newPen); painter.setBrush(QColor(qRgb(255, 255, 255))); painter.drawLine(hLPoint, hRPoint); painter.drawLine(uLPoint, uRPoint);
QPen pen(Qt::white); painter.setPen(pen); QFont font; font.setFamily("Microsoft Yahei"); font.setWeight(65); painter.setFont(font); QTextOption option; option.setAlignment(Qt::AlignCenter); painter.drawText(rect, "设置", option); }
以上已实现重绘两边半圆中间矩形,并可以在这个图形里重绘文字,想要在在两边半圆和中间矩形的那两条边都画上颜色,即如代码里,我已将矩形的上下两边都画成白色了,有什么办法或什么接口能将两边半圆的边框也画成白色吗?并且能不能给的矩形上下两条线和两边半圆的圆弧画的线加粗呢?
各位大佬,劳烦帮忙看看,给点意见,小弟在此多谢了!
|
|