QPainter怎么实现矩形旋转
QPainter painter(this);
QRect rect1(100, 100, 100, 100);
QRect rect2(300, 100, 100, 100);
painter.drawRect(rect1);
rotateAndPaintRect(&painter, rect1, 45);
painter.drawRect(rect2);
rotateAndPaintRect(&painter, rect2, 15);
rotateAndPaintRect(&painter, rect2, 30);
rotateAndPaintRect(&painter, rect2, 45);
rotateAndPaintRect(&painter, rect2, 60);
rotateAndPaintRect(&painter, rect2, 75);
}
// 绕矩形的中心旋转矩形
void Widget::rotateAndPaintRect(QPainter *painter, const QRect &rect, int angle) {
QRect rotatedRect(-rect.width()/2, -rect.height()/2, rect.width(), rect.height());
int cx = rect.x() + rect.width() / 2;
int cy = rect.y() + rect.height() / 2;
painter->save();
painter->translate(cx, cy);
painter->rotate(angle);
painter->drawRect(rotatedRect);
painter->restore();
}
2024-10-21 广告