Qt Don't resize window when scaled QPixmap exceeds bounds

Viewed 72

enter image description here

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);
}
1 Answers

Turns out to be a pretty easy solution, which makes me feel kind of stupid. Using QSizePolicy::Ignored for both hor/vert in contentLabel->setSizePolicy make the window not resize to fit the image label, so the image can scale up outside the widget bounds. As far as being able to resize down once the image was scaled up, grabing the parent widgets size from a resize event and setting the content labels size to it allowed for scaling down the image again.

Related