要用一些小方块来绘制圆形,可以使用QPainter的drawRect()方法来绘制一系列相邻的小矩形,以模拟一个圆形。下面是一个简单的例子:
void MyWidget::paintEvent(QPaintEvent* event)
{
// 创建QPainter对象,并指定绘制的QPaintDevice(通常为QWidget)
QPainter painter(this);
// 设置每个小方块的大小和填充颜色
QSizeF blockSize(5.0, 5.0);
QColor blockColor(Qt::red);
// 绘制小方块来模拟圆形
QRectF boundsRect = event->rect();
QPointF center = boundsRect.center();
qreal radius = qMin(boundsRect.width(), boundsRect.height()) / 2;
qreal x = center.x() - radius;
qreal y = center.y() - radius;
for (int i = x; i < x + 2 * radius; i += blockSize.width()) {
for (int j = y; j < y + 2 * radius; j += blockSize.height()) {
if ((i - center.x()) * (i - center.x()) + (j - center.y()) * (j - center.y()) <= radius * radius) {
QRectF rect(i, j, blockSize.width(), blockSize.height());