2017-12-23 7 views
0

"Google Styled"カードと一致するカスタムウィジェットでペイントしています。私はほとんどの寸法とフォントを正しく持っていますが、画像をペイントすると常に伸びています。ここに参考画像と関連コードがあります。私はそれをデフォルトアスペクト比にしておきたいと思います。Qt - Qアスペクト比でQPixmapをペイントしない

画像:

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"); 

答えて

2

あなたはしかし、painter.drawPixmap機能にもtopPortion変数を渡し、m_image.scaled機能を使用して画像を拡大縮小、およびdocsに応じている:

pixmapと rectanの両方が使用されている場合、pixmapは四角形に合わせてスケーリングされますgleのサイズは不一致です。

だから私のソリューションです:

//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); 
関連する問題