QPainter painter(this); QBrush brush(Qt::gray, Qt::SolidPattern); QRect screen(0, 0, width(), height()); painter.drawRect(screen); painter.fillRect(screen, brush); QBrush blockBrush(Qt::blue, Qt::SolidPattern); QRect block(0, 0, screen.width() / 2 , screen.height() / 2); painter.drawRect(block); painter.fillRect(block, blockBrush);
float ratio = 1; QPainter painter(this); QBrush brush(Qt::gray, Qt::SolidPattern); QRect screen(0, 0, width(), height()); painter.drawRect(screen); painter.fillRect(screen, brush); QBrush blockBrush(Qt::blue, Qt::SolidPattern); int newWidth = screen.width() / 2; int newHeight = screen.height() / 2; if ((int)(newWidth * ratio) < newHeight) { newHeight = newWidth * ratio; } if ((int)(newHeight / ratio) < newWidth) { newWidth = newHeight / ratio; } QRect block(0, 0, newWidth , newHeight); painter.drawRect(block); painter.fillRect(block, blockBrush);