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. QWidget PaintEvent CompositionMode "masking" issue

QWidget PaintEvent CompositionMode "masking" issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 979 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.
  • C Offline
    C Offline
    calier
    wrote on last edited by calier
    #1

    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 !

    0_1520287365638_widget_CompMode.png

    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_())
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      calier
      wrote on last edited by
      #2

      It looks like the CompositionModes only work on QImages. Bummer :/

      0_1520295016614_compMode_doc.png

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

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