Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Fit image into Qlabel

    General and Desktop
    3
    4
    11287
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      md2012 last edited by md2012

      Hello dear programmers.
      Well as the subject says i want to Fit my image in Qlabel but here is the case,
      there is scaleContent for that Qlabel that does the Opposite job of fitting an image.
      it stretches the image in both vertical and horizontal.
      so any idea of how to do the trick?
      i used Pixmap not QPicture:

      QPixmap image(":/new/prefix1/background/main logo_.png");
          ui->label->setPixmap(image);
      
      [Example of fitting an image](http://s4.img7.ir/xgUvJ.png)
      ```![alt text](image url)
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Since you can get the size of the label, you can use QPixmap::scaled to resize the image.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • S
          Suares last edited by

          Hi! If I got it right you should use this.

          For example:

          const int w = label->width();
          const int h = label->height();
          label->setPixmap(pixmap.scaled(w,h,Qt::IgnoreAspectRatio));
          
          1 Reply Last reply Reply Quote 1
          • M
            md2012 last edited by md2012

            @Suares
            @SGaist

            Final eddit ,KeepAspectRatio was the aspect ratio mode that needed for Fitting an image into Qlabel

            QPixmap image("image address");
            h=ui->label->height;
            w=ui->label->width;
            ui->label->setPixmap(image.scaled(w,h,Qt::KeepAspectRatio));
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post