Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9064245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:12:07+00:00 2026-06-16T16:12:07+00:00

I made a class called ImageLabel which extends QLabel. I want it to keep

  • 0

I made a class called ImageLabel which extends QLabel. I want it to keep the size ratio of the image that it is displaying no matter how stretched out it is. It works fine when you make the window larger. The problem comes in when you try to make the window smaller: it doesnt resize the height, it leaves it stretched out. How do I fix this?


int ImageLabel::heightForWidth(int width) const {
    int height = (this->size.height()*width)/this->size.width();
    return height;
}

QSize ImageLabel::sizeHint() const {
    return this->size;
}

QSize ImageLabel::minimumSizeHint() const {
    return QSize(0, 0);
}

void ImageLabel::setSizePolicy(){
    QSizePolicy policy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    policy.setHeightForWidth(true);
    QLabel::setSizePolicy(policy);
    QLabel::setScaledContents(true);
}

void ImageLabel::setPixmap ( const QPixmap &pixmap ){
    this->size = pixmap.size();
    QLabel::setPixmap(pixmap);
}

int main(int argc, char *argv[]){
    QApplication a(argc, argv);

    QFrame *frame = new QFrame;
    QVBoxLayout *layout = new QVBoxLayout;
    frame->setLayout(layout);

    QPixmap map;
    map.load("test.png");
    ImageLabel *label = new ImageLabel;
    label->setSizePolicy();
    label->setPixmap(map);
    layout->addWidget(label);
    frame->show();

    return a.exec();
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:12:08+00:00Added an answer on June 16, 2026 at 4:12 pm

    There are a couple ways to do this, but most of all I would recommend maybe not fighting against the layout system by trying to hint the aspect. As you can see, you are having to try and implement a number methods trying to help the layout.

    I can offer two examples. Neither of them use layouts…

    The first uses a child QLabel to show the image, and drives its fixed size off of the resize event:

    // imagelabel.h
    
    class ImageLabel : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ImageLabel(QWidget *parent = 0);
        const QPixmap* pixmap() const;
    
    public slots:
        void setPixmap(const QPixmap&);
    
    protected:
        void resizeEvent(QResizeEvent *);
    
    private slots:
        void resizeImage();
    
    private:
        QLabel *label;
    };
    
    // imagelabel.cpp
    
    ImageLabel::ImageLabel(QWidget *parent) :
        QWidget(parent)
    {
        label = new QLabel(this);
        label->setScaledContents(true);
        label->setFixedSize(0,0);
    }
    
    void ImageLabel::resizeEvent(QResizeEvent *event) {
        QWidget::resizeEvent(event);
        resizeImage();
    }
    
    const QPixmap* ImageLabel::pixmap() const {
        return label->pixmap();
    }
    
    void ImageLabel::setPixmap (const QPixmap &pixmap){
        label->setPixmap(pixmap);
        resizeImage();
    }
    
    void ImageLabel::resizeImage() {
        QSize pixSize = label->pixmap()->size();
        pixSize.scale(size(), Qt::KeepAspectRatio);
        label->setFixedSize(pixSize);
    }
    

    The second example is based off of the answer given by @Arnold_Spence. It is even shorter as it doesn’t use a child QLabel. It just draws the pixmap in the paint event:

    // imagelabel2.h
    
    class ImageLabel2 : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ImageLabel2(QWidget *parent = 0);
        const QPixmap* pixmap() const;
    
    public slots:
        void setPixmap(const QPixmap&);
    
    protected:
        void paintEvent(QPaintEvent *);
    
    private:
        QPixmap pix;
    };
    
    // imagelabel2.cpp
    
    ImageLabel2::ImageLabel2(QWidget *parent) :
        QWidget(parent)
    {
    }
    
    void ImageLabel2::paintEvent(QPaintEvent *event) {
        QWidget::paintEvent(event);
    
        if (pix.isNull())
            return;
    
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
    
        QSize pixSize = pix.size();
        pixSize.scale(event->rect().size(), Qt::KeepAspectRatio);
    
        QPixmap scaledPix = pix.scaled(pixSize,
                                       Qt::KeepAspectRatio,
                                       Qt::SmoothTransformation
                                       );
    
        painter.drawPixmap(QPoint(), scaledPix);
    
    }
    
    const QPixmap* ImageLabel2::pixmap() const {
        return &pix;
    }
    
    void ImageLabel2::setPixmap (const QPixmap &pixmap){
        pix = pixmap;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question. I made a class called Tester1 which extends another called Tester2. Tester2
I made a class called SpecializationBean which has two private fields: private ArrayList<SelectItem> specializationItems=
What i want is that i have made a picture chooser, the image chosen
I have a problem with classes. I made my own class called Person. Which
I have an Array that is in a class called MusicArray and I want
I have made a helper class called Navigation that gets used on every page
I have a custom made class called Graph in which I use adjacency lists.
I made a class called view2 which is a UIView Controller subclass and when
I made a class which looks something like. class NewInt: def __init__(self, x,y): self.holders
I have made an class which conforms to the NSCoding protocol and does all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.