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. Multiple images on QLabel [Solved]

Multiple images on QLabel [Solved]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 15.2k 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.
  • G Offline
    G Offline
    Gowtam
    wrote on last edited by
    #1

    Hello all,
    I'm trying to display transparent image on QLabel. I also need this checkerred background for showing transparency. But, when I apply pixmap, the background image in the stylesheet is not showing up. The image has opacity. Can anyone explain why?
    I have to show the semi transparent image on the checkered background. The qlabel size keeps varying according to the size of image.
    @
    imageLabel->setStyleSheet("background-image:url(:/checkered.png)");
    imageLabel->setPixmap(image_pixmap);@

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      I suspect that behind the scenes the stylesheet and setPixmap use the same method to set an image for the QLabel therefore your second call simply supersedes your first. The easiest way to achieve what you want is probably to combine your two images into a single QPixmap and use that instead:

      @
      ...
      QLabel *label=new QLabel(this);

      QPixmap *pixmap=new QPixmap(WIDTH, HEIGHT);
      pixmap->fill(Qt::transparent);
      QPainter *painter=new QPainter(pixmap);
      painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/checkered.jpeg"));
      painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/my_image.png"));
      painter->end();

      label->setPixmap(*pixmap);
      label->resize(QSize(WIDTH,HEIGHT));
      ...
      @

      How you derive or calculate WIDTH and HEIGHT is up to you.

      Hope this helps ;o)

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      1
      • G Offline
        G Offline
        Gowtam
        wrote on last edited by
        #3

        Thanks for the reply! Actually, when I set the background-image in stylesheet, it repeats the image automatically according to width and height. To combine the images, I actually have to write a loop for repeating the image and width and height varies lot of times. I'm thinking if there's simple way around.

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          Tiling the 'checkered' image isn't exactly complicated and its exactly what QLabel will be doing for you:

          @
          QLabel *label=new QLabel(this);

          QPixmap *tile=new QPixmap(":/checkered_tile.jpg");
          QPixmap *pixmap=new QPixmap(WIDTH, HEIGHT);
          pixmap->fill(Qt::transparent);
          QPainter *painter=new QPainter(pixmap);
          
          for(int y=0; y<=HEIGHT; y+=TILE){
              for(int x=0; x<=WIDTH; x+=TILE)
                  painter->drawPixmap(x, y, TILE, TILE, *tile);
          }
          painter->drawPixmap(0, 0, WIDTH, HEIGHT, QPixmap(":/my_image.png"));
          painter->end();
          
          label->setPixmap(*pixmap);
          label->resize(QSize(WIDTH,HEIGHT));
          

          @

          N.B. TILE is the both the width and height of my tile image.

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            [quote author="Gowtam" date="1388666787"]
            I have to show the semi transparent image on the checkered background. The qlabel size keeps varying according to the size of image.
            @
            imageLabel->setStyleSheet("background-image:url(:/checkered.png)");
            imageLabel->setPixmap(image_pixmap);@[/quote]
            your code works for me in Qt4 and Qt5.

            Please make sure:

            that the path to the checker image file is correct

            that the image file is contained in the qrc file

            that you rerun qmake and rebuilt your application

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • jazzycamelJ Offline
              jazzycamelJ Offline
              jazzycamel
              wrote on last edited by
              #6

              [quote author="raven-worx" date="1388670433"]
              ...
              your code works for me in Qt4 and Qt5.
              ...
              [/quote]

              Thats embarrassing, guess I should have checked that first too rather than going on what I suspect to be the case. We live and learn I guess ;o) Thanks raven-worx.

              For the avoidance of doubt:

              1. All my code samples (C++ or Python) are tested before posting
              2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                Just for clarification: the code of yours can be pretty simplified:
                @
                QPixmap pix( imagePix.size() );
                QPainter p(&pix);
                p.fillRect( QRect(QPoint(0,0),imagePix.size()), QPixmap(":/checker.png") );
                p.drawPixmap( imagePix );
                p.end();
                //set
                @
                But this is pretty much what the stylesheet code does anyway... just with a polished QPalette.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gowtam
                  wrote on last edited by
                  #8

                  Thanks all! The actual problem was with image pixmap i created. While using painter, I filled with Qt::white, instead of Qt::transparent. Works fine now :)

                  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