QWidget PaintEvent CompositionMode "masking" issue
-
Hi !
I'm trying to use QPainter to draw a custom widget and use the compositionMode as a mask to what I painted.
I tried pretty much all combos and I can't quite figure out how to do it.
Attached is a trimmed down version of my code and a picture to explain my issue.The idea is to paint a few shapes and "mask" the result with another shape.
EDIT: Qt's examples set the "compositionMode" on QImages rather than directly within a QWidget's paintEvent which is a pretty slow method especially when resizing the window, etc ...
I don't see anything in the QPainter's documentation saying that you can't use it the way I am using it. Does anybody knows why this isn't working ?(FYI: my goal isn't to create a simple bar with rounded corners as my screenshots suggests. This is just a stripped down version of my code to demonstrate my problem)
Any help would be greatly appreciated, thanks !
import sys try: import PySide2 from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * except: import PySide from PySide.QtCore import * from PySide.QtGui import * class SomeCustomWidget(QWidget): def __init__(self, parent = None): QWidget.__init__(self , parent) self.setMaximumHeight(28) self.setAttribute(Qt.WA_TranslucentBackground) def paintEvent(self, e): rect = self.rect() painter = QPainter() painter.begin(self) painter.setBackgroundMode(Qt.TransparentMode) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.HighQualityAntialiasing) painter.setCompositionMode(QPainter.CompositionMode_Source) painter.setPen(Qt.NoPen) # Paint Source # Draw a bunch of stuff in there painter.setBrush( QColor(128, 200, 255, 255) ) painter.drawRect(0, 0, rect.width(), rect.height()) # Set Mask to everything drawned above painter.setCompositionMode(QPainter.CompositionMode_SourceIn) painter.setBrush( QColor(255, 255, 255, 255) ) painter.drawRoundedRect(0, 0, rect.width(), rect.height(), 12, 12) painter.end() if __name__ == '__main__': app = QApplication(sys.argv) custWidget = SomeCustomWidget() custWidget.show() sys.exit(app.exec_())
-
Hi,
Depending on what you need, you can draw on a QImage and then draw that image in your custom widget.
What is it that you want to achieve ?