PyQt5: Create semi-transparent window with non-transparent children

后端 未结 1 1565
长情又很酷
长情又很酷 2021-02-19 11:10

I want to create a fullscreen window with semitransparent background, but fully visible children widgets (kind of overlay effect).

Here\'s what I have so far:

         


        
1条回答
  •  逝去的感伤
    2021-02-19 11:32

    Ok, while is seems not to work with the available flags you can still use Qt.WA_TranslucentBackground because it is possible to draw a semitranparent rect on that transparency.

    Derive your mainwindow from QMainWindow and use that class instead.

    Apply self.setAttribute(Qt.WA_TranslucentBackground, True) to that class

    Implement the paintEvent of your mainwindow class like this (similar, might contain errors, but the principle should work):

    QPixmap canvas(rect())
    
    canvas.fill(Qt.transparent) # fill transparent (makes alpha channel available)
    
    QPainter p(canvas)           # draw on the canvas
    p.setOpacity(0.3)
    p.setBrush(QBrush(Qt.white)) # use the color you like
    p.setPen(QPen(Qt.transparen))
    
    p.drawRect(rect()) # draws the canvas with desired opacity
    
    p.start(self)      # now draw on the window itself
    p.drawPixmap(rect(), canvas)
    

    0 讨论(0)
提交回复
热议问题