It is still not clear what you want. For me "scaled to fit" means the ellipse and rectangle should be the size of the widget:
p.setBrush(Qt::red);
p.drawRect(rect());
p.setBrush(Qt::yellow);
p.drawEllipse(rect());
}
void Widget::paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setBrush(Qt::red);
p.drawRect(rect());
p.setBrush(Qt::yellow);
p.drawEllipse(rect());
}
To copy to clipboard, switch view to plain text mode
If you wish to keep the aspect ratio then compare width to height and scale the rectangle to the smaller of the two values.
int side = qMin(width(), height());
QRect r
(0,
0, side, side
);
r.moveCenter(rect().center());
p.drawRect(r);
p.drawEllipse(r);
int side = qMin(width(), height());
QRect r(0,0, side, side);
r.moveCenter(rect().center());
p.drawRect(r);
p.drawEllipse(r);
To copy to clipboard, switch view to plain text mode
Bookmarks