PySide6 for which the parent is responsible in QGraphicsDropShadowEffect



  • Let's eat. QFramefrom which the shadow must fall:

    frame = QFrame(parent, Geometry=QRect(10, 10, 50, 50))
    frame.setGraphicsEffect(QGraphicsDropShadowEffect(centralWidget, blurRadius=15, offset=QPoint(0, 0), color=QColor(0, 0, 0, 200)))
    

    For centralWidget You can write anything. frameAt least QLabel on the frimet, if he was there, in any part of the interface, but without him, there's no shadow.

    What does this parameter have to do with it and what is the right thing to write?

    Thank you.



  • Documentation http://doc.qt.io/qt-5/qwidget.html#setGraphicsEffect It is stated that the same QGraphicsEffect may not be used by other species:

    If the effect is on another type, setGraphicsEffect() http://doc.qt.io/qt-5/qwidget.html#setGraphicsEffect Remove the effect from the view and put it on that view.

    So you'll have to create QGraphicsEffect For each view, http://doc.qt.io/qt-5/qgraphicseffect.html But if you don't want to write a lot of codes and you want to use the same characteristics, You can cross the view and use it. findChildren(...) ♪ http://doc.qt.io/qt-5/qobject.html#findChildren

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    

    class Container(QtWidgets.QMainWindow):
    def init(self, window, parent=None):
    super(Container, self).init(parent)
    self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        centralWidget = QtWidgets.QWidget()
        self.setCentralWidget(centralWidget)
    
        self.label = QtWidgets.QLabel(
            '<h1>Hello World</h1>', alignment=QtCore.Qt.AlignCenter)
        self.label.setFixedSize(140, 30)    
        self.label.setStyleSheet("background-color: transparent;")  
        self.button1 = QtWidgets.QPushButton('Button 1')
        self.button1.setFixedSize(100, 100)
        self.button2 = QtWidgets.QPushButton('Button 2')
        self.button2.setFixedSize(100, 100)
        
        glay = QtWidgets.QGridLayout(window)       
        glay.addWidget(self.label, 0, 0, 1, 2, alignment=QtCore.Qt.AlignCenter)
        glay.addWidget(self.button1, 1, 0, alignment=QtCore.Qt.AlignCenter)
        glay.addWidget(self.button2, 1, 1, alignment=QtCore.Qt.AlignCenter)
        
        lay = QtWidgets.QVBoxLayout(centralWidget)
        lay.addWidget(window)
        lay.setContentsMargins(10, 10, 10, 10)
    

    !!! vvvvvvvvvvvv

        for children in self.findChildren(QtWidgets.QWidget):
            shadow = QtWidgets.QGraphicsDropShadowEffect(
                blurRadius=5, 
                color=QtGui.QColor(99, 255, 255),
                xOffset=3, 
                yOffset=3
            )
            children.setGraphicsEffect(shadow)
    

    !!!

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()
    
    def mouseMoveEvent(self, event):
        delta = QtCore.QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()
    

    class Window(QtWidgets.QWidget):
    def init(self):
    super().init()
    self.setWindowTitle('Icon')
    self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
    self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

    def paintEvent(self, ev):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)        
        painter.setBrush(QtGui.QColor(195, 195, 255))
        painter.setPen(QtCore.Qt.NoPen)
        painter.drawRoundedRect(self.rect(), 10.0, 10.0)
    

    if name == 'main':
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle('fusion')
    w = Window()
    container = Container(w)
    container.resize(640, 480)
    container.show()
    sys.exit(app.exec_())

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2