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. On QLabel the image slides after setmask
Forum Updated to NodeBB v4.3 + New Features

On QLabel the image slides after setmask

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 474 Views
  • 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.
  • KaguroK Offline
    KaguroK Offline
    Kaguro
    wrote on last edited by
    #1

    Hi Guys!
    I tried to create a QLabel with a png with alpha channels like this:

    QLabel *labi = new QLabel;
    labi->setPixmap(QPixmap("madi.png"));
    

    After that, I set the image mask for it because I want the edges of the QLabel to match the edges of the image:

    labi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height(),Qt::KeepAspectRatio,Qt::SmoothTransformation).mask());
    

    I get approximately good results, BUT the image slides on it:
    2024-04-01_20h34_24.png

    What is the correct way to solve this? And where are my mistakes? Sorry if its a really lame question. Thank you in advance for your help!

    JonBJ 1 Reply Last reply
    0
    • KaguroK Kaguro

      @Bonnie Yes thats correct! Thanks! :D And if I want to scalable solution? So if i would like to the pixmap to always adjust to the size of the label?

      B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #6

      @Kaguro If your label still has a fixed size, then just modify your code a little:

      labi->setScaledContents(true);
      //...set fixed size...
      labi->setMask(labi->pixmap().scaled(labi->width(), labi->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation).mask());
      

      In your original code the scaling doesn't work because labi->pixmap() always return the original pixmap, not the scaled one, so the size it returns is not the actually showing size. And also when QLabel scales it ignores aspect ratio.

      If you want the mask can change dynamically with the label size, then it is too complicated.
      You'll need to subclass QLabel and create a scaled pixmap wheneven its size changed and reset the mask.

      KaguroK 1 Reply Last reply
      1
      • KaguroK Kaguro

        Hi Guys!
        I tried to create a QLabel with a png with alpha channels like this:

        QLabel *labi = new QLabel;
        labi->setPixmap(QPixmap("madi.png"));
        

        After that, I set the image mask for it because I want the edges of the QLabel to match the edges of the image:

        labi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height(),Qt::KeepAspectRatio,Qt::SmoothTransformation).mask());
        

        I get approximately good results, BUT the image slides on it:
        2024-04-01_20h34_24.png

        What is the correct way to solve this? And where are my mistakes? Sorry if its a really lame question. Thank you in advance for your help!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Kaguro
        This is a total guess, may be nonsense! If you have labi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height() does that mask have to match the scaling of the original pixmap? Does your label need setScaledContents(true) to match that, or have its pixmap() scaled to fit? Or just ignore this :)

        KaguroK 1 Reply Last reply
        0
        • JonBJ JonB

          @Kaguro
          This is a total guess, may be nonsense! If you have labi->setMask(labi->pixmap().scaled(labi->pixmap().width(),labi->pixmap().height() does that mask have to match the scaling of the original pixmap? Does your label need setScaledContents(true) to match that, or have its pixmap() scaled to fit? Or just ignore this :)

          KaguroK Offline
          KaguroK Offline
          Kaguro
          wrote on last edited by
          #3

          @JonB Yes I wanted to solve with that part that mask can scaling of the original pixmap. I think its pixmap() scaled to fit. I tried with this now:

          QLabel *labi = new QLabel;
          labi->setScaledContents(true);
          labi->setPixmap(QPixmap("madi.png"));
          labi->setMask(labi->pixmap().mask());
          

          And the result:
          2024-04-02_01h26_33.png

          B 1 Reply Last reply
          0
          • KaguroK Kaguro

            @JonB Yes I wanted to solve with that part that mask can scaling of the original pixmap. I think its pixmap() scaled to fit. I tried with this now:

            QLabel *labi = new QLabel;
            labi->setScaledContents(true);
            labi->setPixmap(QPixmap("madi.png"));
            labi->setMask(labi->pixmap().mask());
            

            And the result:
            2024-04-02_01h26_33.png

            B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #4

            @Kaguro What about avoiding scaling and setting the QLabel to a fixed size same as the image size

            KaguroK 1 Reply Last reply
            2
            • B Bonnie

              @Kaguro What about avoiding scaling and setting the QLabel to a fixed size same as the image size

              KaguroK Offline
              KaguroK Offline
              Kaguro
              wrote on last edited by
              #5

              @Bonnie Yes thats correct! Thanks! :D And if I want to scalable solution? So if i would like to the pixmap to always adjust to the size of the label?

              B 1 Reply Last reply
              0
              • KaguroK Kaguro

                @Bonnie Yes thats correct! Thanks! :D And if I want to scalable solution? So if i would like to the pixmap to always adjust to the size of the label?

                B Offline
                B Offline
                Bonnie
                wrote on last edited by Bonnie
                #6

                @Kaguro If your label still has a fixed size, then just modify your code a little:

                labi->setScaledContents(true);
                //...set fixed size...
                labi->setMask(labi->pixmap().scaled(labi->width(), labi->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation).mask());
                

                In your original code the scaling doesn't work because labi->pixmap() always return the original pixmap, not the scaled one, so the size it returns is not the actually showing size. And also when QLabel scales it ignores aspect ratio.

                If you want the mask can change dynamically with the label size, then it is too complicated.
                You'll need to subclass QLabel and create a scaled pixmap wheneven its size changed and reset the mask.

                KaguroK 1 Reply Last reply
                1
                • KaguroK Kaguro has marked this topic as solved on
                • B Bonnie

                  @Kaguro If your label still has a fixed size, then just modify your code a little:

                  labi->setScaledContents(true);
                  //...set fixed size...
                  labi->setMask(labi->pixmap().scaled(labi->width(), labi->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation).mask());
                  

                  In your original code the scaling doesn't work because labi->pixmap() always return the original pixmap, not the scaled one, so the size it returns is not the actually showing size. And also when QLabel scales it ignores aspect ratio.

                  If you want the mask can change dynamically with the label size, then it is too complicated.
                  You'll need to subclass QLabel and create a scaled pixmap wheneven its size changed and reset the mask.

                  KaguroK Offline
                  KaguroK Offline
                  Kaguro
                  wrote on last edited by
                  #7

                  @Bonnie Sorry for late replay i tried it and Yeah you are right! And it works!:) Many thanks!

                  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