Fix image quality when fractional scaling is applied. (#5684)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-03-03 14:40:12 +01:00 committed by GitHub
parent 15725c67c7
commit b706e26a32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -166,21 +166,36 @@ void CardInfoPictureWidget::paintEvent(QPaintEvent *event)
} }
} }
// Adjust scaling after rotation // Handle DPI scaling
const QSize availableSize = size(); // Size of the widget qreal dpr = devicePixelRatio(); // Get the actual scaling factor
const QSize scaledSize = transformedPixmap.size().scaled(availableSize, Qt::KeepAspectRatio); QSize availableSize = size() * dpr; // Convert to physical pixel size
const QRect targetRect{(availableSize.width() - scaledSize.width()) / 2, // Compute final scaled size
(availableSize.height() - scaledSize.height()) / 2, scaledSize.width(), scaledSize.height()}; QSize pixmapSize = transformedPixmap.size();
QSize scaledSize = pixmapSize.scaled(availableSize, Qt::KeepAspectRatio);
const qreal radius = 0.05 * scaledSize.width(); // Pre-scale the pixmap once before drawing
QPixmap finalPixmap = transformedPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
finalPixmap.setDevicePixelRatio(dpr); // Ensure correct display on high-DPI screens
// Compute target rectangle with explicit integer conversion
int targetX = static_cast<int>((availableSize.width() - scaledSize.width()) / (2 * dpr));
int targetY = static_cast<int>((availableSize.height() - scaledSize.height()) / (2 * dpr));
int targetW = static_cast<int>(scaledSize.width() / dpr);
int targetH = static_cast<int>(scaledSize.height() / dpr);
QRect targetRect{targetX, targetY, targetW, targetH};
// Compute rounded corner radius
qreal radius = 0.05 * static_cast<qreal>(targetRect.width()); // Ensure consistent rounding
// Draw the pixmap with rounded corners // Draw the pixmap with rounded corners
QStylePainter painter(this); QStylePainter painter(this);
QPainterPath shape; QPainterPath shape;
shape.addRoundedRect(targetRect, radius, radius); shape.addRoundedRect(targetRect, radius, radius);
painter.setClipPath(shape); painter.setClipPath(shape);
painter.drawPixmap(targetRect, transformedPixmap.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
// Draw the pre-scaled pixmap directly
painter.drawPixmap(targetRect, finalPixmap);
} }
/** /**