Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fit image into Qlabel
Forum Updated to NodeBB v4.3 + New Features

Fit image into Qlabel

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 13.8k Views 1 Watching
  • 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.
  • md2012M Offline
    md2012M Offline
    md2012
    wrote on last edited by md2012
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • S Offline
        S Offline
        Suares
        wrote on last edited by
        #3

        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
        1
        • md2012M Offline
          md2012M Offline
          md2012
          wrote on last edited by md2012
          #4

          @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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved