Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QImage::setAlphaChannel -- obsolete or not?



  • Looking at the current documentation for QImage, the method QImage::setAlphaChannel() is listed with no mention of it being obsolete/deprecated/etc, nor any admonishment against its use in new code:

    void QImage::setAlphaChannel(const QImage &alphaChannel)

    Sets the alpha channel of this image to the given alphaChannel.

    If alphaChannel is an 8 bit alpha image, the alpha values are used directly. Otherwise, alphaChannel is converted to 8 bit grayscale and the intensity of the pixel values is used.

    If the image already has an alpha channel, the existing alpha channel is multiplied with the new one. If the image doesn't have an alpha channel it will be converted to a format that does.

    The operation is similar to painting alphaChannel as an alpha image over this image using QPainter::CompositionMode_DestinationIn.

    See also hasAlphaChannel(), alphaChannel(), Image Transformations, and Image Formats.

    ...Which is weird, because even as far back in Qt 5.11 setAlphaChannel() was listed as an obsolete member for QImage.

    Use one of the composition modes in QPainter::CompositionMode instead.

    Warning: This function is expensive.

    What gives? Did setAlphaChannel() get "un-obsoleted"? Or did it just end up back in the wrong part of the docs? (Perhaps accidentally getting restored to visibility, when it was supposed to be excised entirely?)


  • Lifetime Qt Champion

    setAlphaChannel was un-deprecated in 5.15



  • @Christian-Ehrlicher Aha! Thanks, good to know. I guess that explains this, then:

    >>> from PyQt5 import QtCore, QtGui
    >>> i = QtGui.QImage("/tmp/front3.jpg")
    >>> m = QtGui.QImage("/tmp/mask.png")
    >>> m = m.convertToFormat(QtGui.QImage.Format_Alpha8)
    >>> i.setAlphaChannel(m)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'QImage' object has no attribute 'setAlphaChannel'
    >>> QtCore.PYQT_VERSION_STR
    '5.14.2'
    >>> QtCore.QT_VERSION_STR
    '5.14.2'
    

    Ooh. Then again, maybe not. Looks like Riverbank didn't get a memo. (After installing from the latest wheels...):

    >>> from PyQt5 import QtCore, QtGui
    >>> i = QtGui.QImage("/tmp/front3.jpg")
    >>> m = QtGui.QImage("/tmp/mask.png")
    >>> m = m.convertToFormat(QtGui.QImage.Format_Alpha8)
    >>> i.setAlphaChannel(m)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'QImage' object has no attribute 'setAlphaChannel'
    >>> QtCore.QT_VERSION_STR
    '5.15.2'
    >>> QtCore.PYQT_VERSION_STR
    '5.15.2'
    

    Welp, I've got a bug report to file. Thanks for the help!