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. QGraphicsOpacityEffect with an opacity mask shows transparent areas as black
Qt 6.11 is out! See what's new in the release blog

QGraphicsOpacityEffect with an opacity mask shows transparent areas as black

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.5k 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.
  • M Offline
    M Offline
    MadMan27
    wrote on last edited by
    #1

    Hello everyone :)

    I am creating a QGraphicsOpacityEffect with a QLinearGradient opacity mask and applying the effect to a QGraphicsPixmapItem, as show below;

    QGraphicsPixmapItem* pItem; // please assume pixmap has been properly set

    QGraphicsOpacityEffect* opacity = new QGraphicsOpacityEffect();

    QLinearGradient lg;
    lg.setStart(50,0);
    lg.setFinalStop(0,0);
    lg.setColorAt(0,Qt::black);
    lg.setColorAt(1,Qt::transparent);
    opacity->setOpacityMask(lg);
    opacity->setOpacity(1);

    pItem->setGraphicsEffect(opacity);

    As you can see, I am simply trying to "fade" the edge of the QGraphicsPixmapItem to transparent, however it fades to black, as shown in this picture:

    https://postimg.org/image/42yxsbs8n/

    Questions:

    1. Why is this happening?
    2. How do I make the black areas be transparent?

    Thank you very much!

    M kshegunovK 2 Replies Last reply
    0
    • M MadMan27

      Hello everyone :)

      I am creating a QGraphicsOpacityEffect with a QLinearGradient opacity mask and applying the effect to a QGraphicsPixmapItem, as show below;

      QGraphicsPixmapItem* pItem; // please assume pixmap has been properly set

      QGraphicsOpacityEffect* opacity = new QGraphicsOpacityEffect();

      QLinearGradient lg;
      lg.setStart(50,0);
      lg.setFinalStop(0,0);
      lg.setColorAt(0,Qt::black);
      lg.setColorAt(1,Qt::transparent);
      opacity->setOpacityMask(lg);
      opacity->setOpacity(1);

      pItem->setGraphicsEffect(opacity);

      As you can see, I am simply trying to "fade" the edge of the QGraphicsPixmapItem to transparent, however it fades to black, as shown in this picture:

      https://postimg.org/image/42yxsbs8n/

      Questions:

      1. Why is this happening?
      2. How do I make the black areas be transparent?

      Thank you very much!

      M Offline
      M Offline
      MadMan27
      wrote on last edited by
      #2

      @MadMan27 bump...anyone have any ideas? :)

      1 Reply Last reply
      0
      • M MadMan27

        Hello everyone :)

        I am creating a QGraphicsOpacityEffect with a QLinearGradient opacity mask and applying the effect to a QGraphicsPixmapItem, as show below;

        QGraphicsPixmapItem* pItem; // please assume pixmap has been properly set

        QGraphicsOpacityEffect* opacity = new QGraphicsOpacityEffect();

        QLinearGradient lg;
        lg.setStart(50,0);
        lg.setFinalStop(0,0);
        lg.setColorAt(0,Qt::black);
        lg.setColorAt(1,Qt::transparent);
        opacity->setOpacityMask(lg);
        opacity->setOpacity(1);

        pItem->setGraphicsEffect(opacity);

        As you can see, I am simply trying to "fade" the edge of the QGraphicsPixmapItem to transparent, however it fades to black, as shown in this picture:

        https://postimg.org/image/42yxsbs8n/

        Questions:

        1. Why is this happening?
        2. How do I make the black areas be transparent?

        Thank you very much!

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @MadMan27
        Hello,

        Why is this happening?

        Because your gradient contains a black color.

        How do I make the black areas be transparent?

        What should be there? If you don't want to have black, drop the opacity effect and just paint those areas as transparent.
        For reference look at the painter's composition modes.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        M 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @MadMan27
          Hello,

          Why is this happening?

          Because your gradient contains a black color.

          How do I make the black areas be transparent?

          What should be there? If you don't want to have black, drop the opacity effect and just paint those areas as transparent.
          For reference look at the painter's composition modes.

          Kind regards.

          M Offline
          M Offline
          MadMan27
          wrote on last edited by MadMan27
          #4

          @kshegunov
          Hello :)

          Thank you for answering :) but I think you completely misunderstood my question. The black color does not matter at all, I am just using a black to transparent gradient to create an opacity MASK (which defines which parts of the image should be opaque). I could create the same mask by using ANY other color, it still shows up as black. It is simply an opacity mask.

          Thanks for the answer though! I hope someone knows why Qt is showing areas that SHOULD be transparent as black...
          It seems like the QGraphicsOpacityEffect isn't working properly.

          M 1 Reply Last reply
          0
          • M MadMan27

            @kshegunov
            Hello :)

            Thank you for answering :) but I think you completely misunderstood my question. The black color does not matter at all, I am just using a black to transparent gradient to create an opacity MASK (which defines which parts of the image should be opaque). I could create the same mask by using ANY other color, it still shows up as black. It is simply an opacity mask.

            Thanks for the answer though! I hope someone knows why Qt is showing areas that SHOULD be transparent as black...
            It seems like the QGraphicsOpacityEffect isn't working properly.

            M Offline
            M Offline
            MadMan27
            wrote on last edited by
            #5

            For anyone still wondering how to do this, I figured out what the problem, and solution is.

            The problem was that the pixmap of the QGraphicsPixmapItem did not have an alpha channel. So when the QGraphicsOpacityEffect tried to make certain pixels transparent, we saw black instead.

            The solution is simply to add an alpha channel to the pixmap. I am sure there are several ways of doing this, but I simply converted the QPixmap to a QImage, and then traversed each pixel of the image and set the QColor to a color that included alpha values.

            Worked like a charm :)

            1 Reply Last reply
            1

            • Login

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