Important: Please read the Qt Code of Conduct -

How to set opacity of a QImage

  • Hello,
    I'd like to know what is the best way to set an opacity of a QImage.

    I know there is setOpacity(int) function defined in QPixmap class but I work with QImage..

    Thank you.

  • Lifetime Qt Champion


    AFAIK, you would need to use an ARGB format. Then it depends on what you are currently doing with that QImage, painting ?

  • You can also apply the opacity at the time of painting, if that's what you are doing.

  • Thanks for your reply ,

    In fact, I have a slider "Opacity" and I want to modify opacity of a QImage and see directly the result.

  • How do you show the image in the first place? What widget are you using to display it?

  • I have a QLabel with a QImage (base image ) and I want to drag others QImage on this( anomaly image like triangle, circle, square, etc). During dropEvent, I copy all pixels of the anomaly image on the base image into the rectangle where it is dropped. So I save a finally image which is the baseImage containing the anomaly.

    Currently I can set opacity of the default image pasted on the base image, I can see it directly, this is my function.
    @void AjoutDefaut::opacity(QImage& image)
    m_method = 9;
    if ( m_A>255 || m_A<0 )
    QPixmap transparent(image.size());
    QPainter p;

    p.drawPixmap(0, 0, QPixmap::fromImage(image));
    p.fillRect(transparent.rect(), QColor(0, 0, 0, m_A));
    image = transparent.toImage();
    //m_currentAnomaly = image;


    I have the result that I want. ( transparency setted )
    And, this is my function which save now the image after setting the anomaly opacity:
    QImage DragWidgetCible::insertAnomaly( QImage base )
    QImage result = QImage();
    result = base.copy(0, 0, base.width(), base.height());
    for(int elem=0; elem<m_anomalyContainer.size(); elem++)
    binariseAnomaly(m_anomalyContainer[elem].getAnomaly(), 1);
    int width = m_anomalyContainer[elem].getAnomaly().width();
    int height = m_anomalyContainer[elem].getAnomaly().height();
    QPoint p(m_anomalyContainer[elem].getAnomalyPos());

     for(int i=p.x(),k=0; i<p.x()+width, k<width; i++,k++)
       for(int j=p.y(), l=0; j<p.y()+height, l<height; j++, l++)
          QRgb x = m_anomalyContainer[elem].getAnomaly().pixel(k, l);
          qDebug() << qGray(x);
              if (qAlpha(x) != 0 && (qGray(x) != 255))
                    result.setPixel(i, j, x);
          else (// we are in this case because  m_opacityUpdated = true )
              result.setPixel(i, j, x);
              m_opacityUpdated = false;

    return result;

    BUT, when I see the final saved image, I have another kind of opacity: a dark image, and when the opacity is setted to 0, the anomaly is black.

  • This works for me. I hope helps

    @QImage image;
    //If the image doesn't have set the alpha chanel
    image = image.convertToFormat(QImage::Format_ARGB32);

    QImage image2;
    image2 = setOpacity(image, 0.5);

    QImage setOpacity(QImage& image, qreal opacity)
    QImage newImg(image.size(), QImage::Format_ARGB32);

    QPainter painter(&newImg);
    painter.drawImage(QRect(0, 0, image.width(), image.height()), image);
    return newImg;


Log in to reply