I'm using a QPixmap to show an image inside a QLabel, and when the image is scaled up exceeding the bounds of the parent widgets layout it's currently in, the window resizes to fit it again, then I also can't make the window smaller and scale down the image. Is there a specific QSizePolicy I need to put on the parent main windows layout, or the label itself, or some other set function?
Or is a solution that instead of scaling the image content up so it eventually exceeds the label bounds, I need to be cropping in the image at a certain point so it's never outside of the label?
w_content = new Content;
QStackedLayout *s_layout = new QStackedLayout(contentContainer);
s_layout->setStackingMode(QStackedLayout::StackAll);
s_layout->setSpacing(0);
s_layout->setContentsMargins(0, 0, 0, 0);
s_layout->addWidget(w_content);
Content::Content(QWidget *parent) : QWidget(parent), contentLabel(new QLabel) {
QVBoxLayout *c_layout = new QVBoxLayout(this);
contentLabel->setBackgroundRole(QPalette::Base);
contentLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
contentLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
contentLabel->setScaledContents(false);
contentImage = new QImage("C:\\Users\\user\\Downloads\\test.png");
contentPixmap = QPixmap::fromImage(*contentImage);
contentLabel->setPixmap(contentPixmap);
c_layout->setSpacing(0);
c_layout->setContentsMargins(0, 0, 0, 0);
c_layout->addWidget(contentLabel);
this->setLayout(c_layout);
}
void Content::scaleImage(double factor) {
m_scaleFactor *= factor;
w = contentPixmap.width()*m_scaleFactor;
h = contentPixmap.height()*m_scaleFactor;
QPixmap pm = contentPixmap.scaled(w, h, Qt::KeepAspectRatio, Qt::FastTransformation);
contentLabel->setPixmap(pm);
}
