#include "RatioAwareLabel.h" RatioAwareLabel::RatioAwareLabel(QWidget *parent) : QLabel(parent) { setScaledContents(false); } void RatioAwareLabel::setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; scale(); } int RatioAwareLabel::heightRatio(int width) const { if (m_pixmap.isNull()) return height(); return (m_pixmap.height() * width / m_pixmap.width()); } QSize RatioAwareLabel::sizeHint() const { int w = width(); return QSize(w, heightRatio(w)); } void RatioAwareLabel::scale() { if (m_pixmap.isNull()) return; QLabel::setPixmap(m_pixmap.scaled(size(), Qt::KeepAspectRatio, Qt::FastTransformation)); } void RatioAwareLabel::resizeEvent(QResizeEvent *event) { QLabel::resizeEvent(event); scale(); }