Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPixmap with transparent background
Qt 6.12 LTS is out! See what's new in the release blog

QPixmap with transparent background

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

    I have a widget whose ctor contains:

    setAttribute(Qt::WA_TransparentForMouseEvents);
    setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_WState_ExplicitShowHide);
    

    it has a null paintEvent handler

    void EditStars::paintEvent([[maybe_unused]] QPaintEvent* event)
    {
        event.ignore();
    }
    

    and the mouseMoveEvent and a few other places do this:

    drawOnPixmap();
    
    imageView->setOverlayPixmap(pixmap);
    imageView->update();
    

    The drawOnPixmap() code does:

    QRect rcClient{ rect() };
    size_t	width = rcClient.width(), height = rcClient.height();
    
    QPainter painter(&pixmap);
    //QPalette palette{ QGuiApplication::palette() };
    QBrush brush{ Qt::transparent };
    painter.setBrush(brush);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.eraseRect(rcClient);
    painter.fillRect(rcClient, brush);
    

    the paintEvent handler of imageView does most of its other works then does:

    painter.setBackgroundMode(Qt::TransparentMode);
    painter.drawPixmap(0, 0, *pOverlayPixmap);
    

    but the overlay pixmap isn't displayed with a transparent background so I can't see through it to the underlying widget :(

    How to get this to work?

    Thanks
    D.

    1 Reply Last reply
    1
    • PerdrixP Perdrix

      I'm not building the pixmap from a png, it's all done with QPainter. - that may make all the difference.

      I am doing this on Windows

      D.

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

      @Perdrix
      well it might
      do you do
      pix.fill(Qt::transparent);
      to have it transparent from the beginning. ?

      1 Reply Last reply
      3
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #2

        I even tried moving the pixmap from being a class instance variable to a local variable
        in drawOnPixmap, omitting the eraseRect() call and adding the calls

        imageView->setOverlayPixmap(pixmap);
        imageView->update();
        

        to the end of mf.

        but that didn't work either :(

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

          Hi
          what type is imageView. ?
          Some widgets like QStacked and QScrollArea have a background of their own.

          PerdrixP 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            what type is imageView. ?
            Some widgets like QStacked and QScrollArea have a background of their own.

            PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by Perdrix
            #4

            @mrjj It is a direct sub-class of QWidget. In the paint mf of ImageView, I'm just overlaying the pixmap that was set by setOverlayPixmap ...

            I tried without the use of the overlay pixmap - instead using the paint mf of the overlaying widget to draw over the underlying image but had the same problem :(
            David

            mrjjM 1 Reply Last reply
            0
            • PerdrixP Perdrix

              @mrjj It is a direct sub-class of QWidget. In the paint mf of ImageView, I'm just overlaying the pixmap that was set by setOverlayPixmap ...

              I tried without the use of the overlay pixmap - instead using the paint mf of the overlaying widget to draw over the underlying image but had the same problem :(
              David

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

              @Perdrix

              What platform ?
              I tried fast on Windows and it does draw it transparent.

              alt text

              Also have you tested your image with a QLabel that it does show up transparent ?

              1 Reply Last reply
              0
              • PerdrixP Offline
                PerdrixP Offline
                Perdrix
                wrote on last edited by
                #6

                I'm not building the pixmap from a png, it's all done with QPainter. - that may make all the difference.

                I am doing this on Windows

                D.

                mrjjM 1 Reply Last reply
                0
                • PerdrixP Perdrix

                  I'm not building the pixmap from a png, it's all done with QPainter. - that may make all the difference.

                  I am doing this on Windows

                  D.

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

                  @Perdrix
                  well it might
                  do you do
                  pix.fill(Qt::transparent);
                  to have it transparent from the beginning. ?

                  1 Reply Last reply
                  3
                  • PerdrixP Offline
                    PerdrixP Offline
                    Perdrix
                    wrote on last edited by Perdrix
                    #8

                    @Perdrix said in QPixmap with transparent background:

                    painter.fillRect(rcClient, brush);

                    No - see code posted above - I used painter.fillRect using a transparent brush

                    C 1 Reply Last reply
                    0
                    • PerdrixP Perdrix

                      @Perdrix said in QPixmap with transparent background:

                      painter.fillRect(rcClient, brush);

                      No - see code posted above - I used painter.fillRect using a transparent brush

                      C Offline
                      C Offline
                      candy76041820
                      wrote on last edited by
                      #9

                      @Perdrix Perhaps filling a rect with transparent brush means painting a transparent rect onto the canvas, which actually is a no-op.

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        The default mode for painter is to blend the painted shape with existing pixels using the alpha channel, so as @candy76041820 said - filling a rectangle with transparency does nothing.

                        To replace the contents of that rectangle instead of blending you have to change composition mode to QPainter::CompositionMode_Source, or, simpler, as @mrjj suggested use fill() method of QPixmap.

                        1 Reply Last reply
                        3
                        • PerdrixP Offline
                          PerdrixP Offline
                          Perdrix
                          wrote on last edited by Perdrix
                          #11

                          Using pixmap.fill(Qt::transparent); solved the problem

                          Thanks a lot

                          1 Reply Last reply
                          2

                          • Login

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