Search code examples
c++qtqpainter

Qt - QPainter not painting QPixmap w/ Aspect Ratio


I am painting in a custom widget to match the "Google-Styled" cards. I have most of the dimensions and font correct however upon painting an image it is always stretched. Here is a reference image and the relevant code. I would like to keep the image at it's default aspect ratio.

Image:

enter image description here

    QRect topPortion = QRect(QPoint(0, 0), QSize(width(), (height()/4)*3));
    QPainterPath backgroundPath;
    backgroundPath.addRect(topPortion);
    QPainterPath bottom = getCornerPath().subtracted(backgroundPath);
    QRect bottomRect = QRegion(rect()).subtracted(QRegion(topPortion)).boundingRect();

    painter.fillPath(getCornerPath(), m_bColor);
    painter.fillPath(bottom, m_fColor);

    painter.drawPixmap(topPortion, m_image.scaled(topPortion.size(), Qt::KeepAspectRatio, Qt::FastTransformation));//Issue

    painter.setPen(QPen(QColor(50, 50, 50)));
    painter.setFont(titleFont);
    painter.drawText(QPointF(12, topPortion.height()+((bottomRect.height()-fontHeight)/2)+QFontMetrics(titleFont).ascent()), "Add Record");
    painter.setFont(subtitleText);
    painter.drawText(QPointF(12, topPortion.height()+((bottomRect.height()-fontHeight)/2)+fontHeight), "Add Record");

Solution

  • You're scaling your image with m_image.scaled function, however passing also to painter.drawPixmap function, the topPortion variable, and according to docs:

    The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.

    So my solution is:

    //Your's calculation area
    QRect topPortion = QRect(QPoint(0, 0), QSize(width(), (height() / 4) * 3));
    
    QPixmap pixmap = QPixmap(1024, 768); //Random image
    pixmap.fill(Qt::red); //Random color
    
    //Scaled size that will be used to set draw aera to QPainter, with aspect ratio preserved
    QSize size = pixmap.size().scaled(topPortion.size(), Qt::KeepAspectRatio);
    
    //Draw the pixmap inside the scaled area, with aspect ratio preserved
    painter.drawPixmap(topPortion.x(), topPortion.y(), size.width(), size.height(), pixmap);