Search code examples
pythonpython-3.xpyqtpyqt5qpainter

QPainter blend color


Th QPainter drawns are superposed when drawn. How can I mix colors when drawing? For example: If there is a green line drawn and a red line is drawn afterwards in the same position, the colour of the line will be red, I would like to get a mix of red and green.


Solution

  • The colors are not superimposed but painted over, and that happens when they are opaque.
    As soon as you use a color with an alpha value less than 255, the colors are "mixed".

    color circles

    pixmap = QtGui.QPixmap(200, 200)
    pixmap.fill(QtCore.Qt.black)
    qp = QtGui.QPainter(pixmap)
    qp.setRenderHints(qp.Antialiasing)
    qp.setPen(QtCore.Qt.NoPen)
    qp.setBrush(QtGui.QColor(255, 0, 0, 85))
    qp.drawEllipse(40, 0, 120, 120)
    qp.setBrush(QtGui.QColor(0, 255, 0, 85))
    qp.drawEllipse(0, 80, 120, 120)
    qp.setBrush(QtGui.QColor(0, 0, 255, 85))
    qp.drawEllipse(80, 80, 120, 120)
    qp.end()