또한 위에 주어진 간단한 것을 요청했기 때문에 나는 내 자신의 질문에 답할 것이지만 해결책으로 표시하지는 않을 것입니다. 어쨌든 너무 간단한 해결책을 사용하여 결국 아무 것도 비슷하게해야 할 사람이 있고 여기에 나와 함께 할 시간이 있습니다. 마지막 작업 코드입니다. 아이디어는 QLabel을 확장하고 setPixmap 및 drawEvent 메소드를 오버로드하는 것입니다.
QPictureLabel.hpp (헤더 파일)
#include "QImage.h"
#include "QPixmap.h"
#include "QLabel.h"
class QPictureLabel : public QLabel
{
private:
QPixmap _qpSource; //preserve the original, so multiple resize events won't break the quality
QPixmap _qpCurrent;
void _displayImage();
public:
QPictureLabel(QWidget *aParent) : QLabel(aParent) { }
void setPixmap(QPixmap aPicture);
void paintEvent(QPaintEvent *aEvent);
};
QPictureLabel.cpp (이행)
#include "QPainter.h"
#include "QPictureLabel.hpp"
void QPictureLabel::paintEvent(QPaintEvent *aEvent)
{
QLabel::paintEvent(aEvent);
_displayImage();
}
void QPictureLabel::setPixmap(QPixmap aPicture)
{
_qpSource = _qpCurrent = aPicture;
repaint();
}
void QPictureLabel::_displayImage()
{
if (_qpSource.isNull()) //no image was set, don't draw anything
return;
float cw = width(), ch = height();
float pw = _qpCurrent.width(), ph = _qpCurrent.height();
if (pw > cw && ph > ch && pw/cw > ph/ch || //both width and high are bigger, ratio at high is bigger or
pw > cw && ph <= ch || //only the width is bigger or
pw < cw && ph < ch && cw/pw < ch/ph //both width and height is smaller, ratio at width is smaller
)
_qpCurrent = _qpSource.scaledToWidth(cw, Qt::TransformationMode::FastTransformation);
else if (pw > cw && ph > ch && pw/cw <= ph/ch || //both width and high are bigger, ratio at width is bigger or
ph > ch && pw <= cw || //only the height is bigger or
pw < cw && ph < ch && cw/pw > ch/ph //both width and height is smaller, ratio at height is smaller
)
_qpCurrent = _qpSource.scaledToHeight(ch, Qt::TransformationMode::FastTransformation);
int x = (cw - _qpCurrent.width())/2, y = (ch - _qpCurrent.height())/2;
QPainter paint(this);
paint.drawPixmap(x, y, _qpCurrent);
}
사용 : 화상 wirthout을 표시하기위한 통상 라벨을 사용하는 것과 setScaledContents
img_Result = new QPictureLabel(ui.parent);
layout = new QVBoxLayout(ui.parent);
layout->setContentsMargins(11, 11, 11, 11);
ui.parent->setLayout(layout);
layout->addWidget(img_Result);
//{...}
QPixmap qpImage(qsImagePath);
img_Result->setPixmap(qpImage);
을하지만, 즉 (이미지를 스트레칭으로 나에게 꽤 추한 결과를 주었다하지 종횡비 유지). 어쨌든 이것을 해결책으로 표시 할 것입니다. 감사. – SinistraD
PyQt5를 사용하면이 솔루션이 작동하지 않습니다. 'setScaledContents'는 표시된 이미지 크기에 아무런 영향을주지 않습니다. – ely