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. Scaling QPixmap
Qt 6.11 is out! See what's new in the release blog

Scaling QPixmap

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 4.3k Views 2 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hello everyone.
    First of all thanks a lot for helping me and reading this post.

    I have a jpeg image as a QPixmap in a scene.
    I would like that the QPixmap has a specific size.

    I try this:

    
                img_scene.scaledToHeight(my_height);
                img_scene.scaledToWidth(my_width);
                //img_scene.scaled(my_height,my_width);
                scene->addPixmap(img_scene);
    

    but it works in the same way than if I do not write scaled...
    Any idea to do that?

    thanks a lot.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AlvaroS said:
      Hi
      Its a classic.
      Scaled returns a copy ! it do not change the original.

      img_scene = img_scene.scaled(my_height,my_width);

      will do it.

      AlvaroSA 1 Reply Last reply
      2
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @AlvaroS
        Hello,
        You're not actually changing the pixmap. Look at the documentation of QPixmap::scaledToHeight. Try something like this:

        img_scene = img_scene.scaledToHeight(my_height);
        img_scene = img_scene.scaledToWidth(my_width);
        

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        3
        • mrjjM mrjj

          @AlvaroS said:
          Hi
          Its a classic.
          Scaled returns a copy ! it do not change the original.

          img_scene = img_scene.scaled(my_height,my_width);

          will do it.

          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #4

          @mrjj said:

          @AlvaroS said:
          Hi
          Its a classic.
          Scaled returns a copy ! it do not change the original.

          img_scene = img_scene.scaled(my_height,my_width);

          will do it.

          @kshegunov said:

          @AlvaroS
          Hello,
          You're not actually changing the pixmap. Look at the documentation of QPixmap::scaledToHeight. Try something like this:

          img_scene = img_scene.scaledToHeight(my_height);
          img_scene = img_scene.scaledToWidth(my_width);
          

          Kind regards.

          Thanks a lot for answering me.
          Now it works but not fine.

          I have some items in my scene and I would like that items appear in foreground so over the QPixmap.

          How can I do that?
          Thanks again!!

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            hi
            try with
            http://doc.qt.io/qt-5/qgraphicsitem.html#setZValue

            AlvaroSA 1 Reply Last reply
            0
            • mrjjM mrjj

              hi
              try with
              http://doc.qt.io/qt-5/qgraphicsitem.html#setZValue

              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on last edited by
              #6

              @mrjj Thanks a lot my friend!! It works!!!

              Now I have another problem... sorry for being boring.

              The image that I add is a irregular square, something like: http://www.maspa.se/SPANSKA/Matematica4/Geometria/Poligonos/Figuras/cuadrilatero1.gif

              But in the scene if I write:
              img_scene = img_scene.scaledToHeight(my_height);
              img_scene = img_scene.scaledToWidth(my_width);

              The image appears like a perfect regular square like: http://science-all.com/images/square/square-03.jpg

              However if I do not write the scaled it appears fine....

              Any ideas what am I doing wrong?!

              Thanks a lot

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                ehh
                Im not sure how scaling the
                polygon image could ever make it a rect !?!?

                AlvaroSA 1 Reply Last reply
                0
                • mrjjM mrjj

                  ehh
                  Im not sure how scaling the
                  polygon image could ever make it a rect !?!?

                  AlvaroSA Offline
                  AlvaroSA Offline
                  AlvaroS
                  wrote on last edited by
                  #8

                  @mrjj i don't know but it scales as a rect..

                  mrjjM 1 Reply Last reply
                  0
                  • AlvaroSA AlvaroS

                    @mrjj i don't know but it scales as a rect..

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @AlvaroS
                    try with other image. :)
                    just for test.

                    AlvaroSA 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @AlvaroS
                      try with other image. :)
                      just for test.

                      AlvaroSA Offline
                      AlvaroSA Offline
                      AlvaroS
                      wrote on last edited by
                      #10

                      @mrjj Yes!! It seems that "scaled" not scale whole image... maybe?

                      mrjjM 1 Reply Last reply
                      0
                      • AlvaroSA AlvaroS

                        @mrjj Yes!! It seems that "scaled" not scale whole image... maybe?

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @AlvaroS
                        Hmm i was think we only saw some of it ?
                        so it looked like rect. :)

                        AlvaroSA 2 Replies Last reply
                        0
                        • mrjjM mrjj

                          @AlvaroS
                          Hmm i was think we only saw some of it ?
                          so it looked like rect. :)

                          AlvaroSA Offline
                          AlvaroSA Offline
                          AlvaroS
                          wrote on last edited by
                          #12

                          @mrjj Yes, it seems what I said yesterday, it does not scale the whole image...
                          Maybe I have to use a Qt::Transformation or something like that...
                          The other idea that I have watched on the internet is to use Qlabel, but I need to use Scene because I have items there...

                          1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @AlvaroS
                            Hmm i was think we only saw some of it ?
                            so it looked like rect. :)

                            AlvaroSA Offline
                            AlvaroSA Offline
                            AlvaroS
                            wrote on last edited by
                            #13

                            @mrjj said:

                            @AlvaroS
                            Hmm i was think we only saw some of it ?
                            so it looked like rect. :)

                            @AlvaroS said:

                            @mrjj Yes, it seems what I said yesterday, it does not scale the whole image...
                            Maybe I have to use a Qt::Transformation or something like that...
                            The other idea that I have watched on the internet is to use Qlabel, but I need to use Scene because I have items there...

                            I have tried this:

                                        img_scene=img_scene.scaled(my_height,my_width, Qt::IgnoreAspectRatio, Qt::FastTransformation);
                                        label_img->setPixmap(img_scene);
                                        label_img->setScaledContents(true);
                                        scene->addWidget(label_img)
                            

                            But it works in the same way..

                            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