2017-12-23 17 views
0

"Google 스타일"카드와 일치하도록 사용자 정의 위젯에 페인팅합니다. 나는 대부분의 치수와 글꼴을 가지고 있지만 그림을 그릴 때 항상 늘린다. 여기에 참조 이미지와 관련 코드가 있습니다. 기본 가로 세로 비율로 이미지를 유지하고 싶습니다.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);