一、绘四角圆角的矩形
略。有空了写
二、绘上方两个为圆角,下面两个为直角矩形
关键函数 QPainter::arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
/*
*void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
* 参数 QRectF &rect 圆角外切矩形大小,宽度为圆角半径的2倍
* 参数 qreal startAngle 开始角度
* 参数 qreal arcLength 运行角度
*/
例效果:
QPainterPath xxx::drawTopRadius(QRect &rect)
{
QPainterPath path;
//设置圆角半径
const qreal radius = 4;
//设置起点为矩形左上圆角圆心
path.moveTo(rect.topLeft().x() + radius, rect.topLeft().y() + radius);
//绘制圆角 圆弧以外切圆的90度位置为起点,逆时针画圆弧运行90度结束(从12点钟方向 - 9点钟方向)
path.arcTo(QRect(rect.topLeft(), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.topRight().x(), rect.topRight().y() - radius * 2);
//画圆弧 (3点钟方向 - 12点钟方向)
path.arcTo(QRect(QPoint(rect.topRight().x() - (radius * 2), rect.topRight().y()), QSize(radius * 2, radius * 2)), 0, 90);
path.lineTo(rect.topLeft().x() + radius, rect.topLeft().y());
return path;
}
三、两端为圆角的椭圆矩形
QPainter paint(this);
paint.setRenderHints(QPainter::HighQualityAntialiasing
| QPainter::SmoothPixmapTransform
| QPainter::Antialiasing);
QPainterPath path;
//移动到起点
path.moveTo(height() / 2, 0);
//以90度起点,逆时针画270度
path.arcTo(QRect(0, 0, height(), height()), 90, 180);
path.lineTo(width() - (height() / 2), height());
//以270度起点,逆时针画180度
path.arcTo(QRect(QPoint(width() - height(), 0), QSize(height(), height())), 270, 180);
path.lineTo(height() / 2, 0);
paint.fillPath(path, QBrush(QColor(0, 0, 0, 15)));
四、根据圆角属性获取不同位置的圆角
enum RadiusProperty {
Radius_Left = 0, //左侧圆角
Radius_Right, //右侧圆角
Radius_Top, //顶部圆角
Radius_Bottom, //底部圆角
Radius_No //无圆角
};
QPainterPath drawRadiusPath(QRect &rect, const RadiusProperty &property)
{
QPainterPath path;
//设置圆角半径
const qreal radius = 20;
if (property == Radius_Top) {
//设置起点为矩形左上圆角圆心
path.moveTo(rect.topLeft().x() + radius, rect.topLeft().y() + radius);
//绘制圆角 圆弧以外切圆的90度位置为起点,逆时针画圆弧运行90度结束(从12点钟方向 - 9点钟方向)
path.arcTo(QRect(rect.topLeft(), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rect.bottomLeft());
path.lineTo(rect.bottomRight());
path.lineTo(rect.topRight().x(), rect.topRight().y() - radius * 2);
//画圆弧 (3点钟方向 - 12点钟方向)
path.arcTo(QRect(QPoint(rect.topRight().x() - (radius * 2), rect.topRight().y()), QSize(radius * 2, radius * 2)), 0, 90);
path.lineTo(rect.topLeft().x() + radius, rect.topLeft().y());
} else if (property == Radius_Bottom) {
} else if (property == Radius_Left) {
path.moveTo(rect.topLeft().x() + radius, rect.topLeft().y() + radius);
path.arcTo(QRect(rect.topLeft(), QSize(radius * 2, radius * 2)), 90, 90);
path.lineTo(rect.x(), rect.height() - (radius * 2));
path.arcTo(QRect(QPoint(rect.x(), rect.height() - radius * 2), QSize(radius * 2, radius * 2)), 180, 90);
path.lineTo(rect.bottomRight());
path.lineTo(rect.topRight());
path.lineTo(rect.x() + radius, rect.y());
} else if (property == Radius_Right) {
path.moveTo(rect.bottomRight().x() - radius, rect.bottomRight().y() - radius);
path.arcTo(QRect(QPoint(rect.width() - radius * 2, rect.height() - radius * 2), QSize(radius * 2, radius * 2)), 270, 90);
path.lineTo(rect.width(), rect.height() - radius);
path.arcTo(QRect(QPoint(rect.width() - radius * 2, rect.y()), QSize(radius * 2, radius * 2)), 0, 90);
path.lineTo(rect.topLeft());
path.lineTo(rect.bottomLeft());
path.lineTo(rect.width() - radius, rect.height());
} else {
path.addRect(rect);
}
return path;
}