-
UID:202628
-
- 注册时间2020-05-17
- 最后登录2024-04-08
- 在线时间445小时
-
- 发帖395
- 搜Ta的帖子
- 精华0
- 金钱5770
- 威望436
- 贡献值0
- 好评度356
-
访问TA的空间加好友用道具
|
如题,在项目 开发中,自己想 重绘一个两边半圆中间矩形的 按钮,具体代码如下: - 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);
- }
以上已实现重绘两边半圆中间矩形,并可以在这个 图形里重绘文字,想要在在两边半圆和中间矩形的那两条边都画上 颜色,即如代码里,我已将矩形的上下两边都画成白色了,有什么办法或什么接口能将两边半圆的边框也画成白色吗?并且能不能给的矩形上下两条线和两边半圆的圆弧画的线加粗呢? 各位大佬,劳烦帮忙看看,给点意见,小弟在此多谢了!
|