将 QGraphicsDropShadowEffect 与多个小部件一起使用
Using QGraphicsDropShadowEffect with multiple widgets
我想使用 QGraphicsDropShadowEffect 在几个小部件上设置阴影,我想知道是否有更好的方法可以做到这一点,而不必为我想使用它的每个实例一遍又一遍地编写相同的代码在我下面的例子中。是否可以创建一个 class 或调用的东西,以便我只需要在小部件上设置 setGraphicsEffect()
?我尝试为它创建一些 classes,但我仍然只能让它们创建一个阴影。
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout()
self.shadow = QGraphicsDropShadowEffect()
self.shadow.setBlurRadius(5)
self.shadow.setXOffset(3)
self.shadow.setYOffset(3)
self.shadow2 = QGraphicsDropShadowEffect()
self.shadow2.setBlurRadius(5)
self.shadow2.setXOffset(3)
self.shadow2.setYOffset(3)
self.btn = QPushButton("Button")
self.btn.setGraphicsEffect(self.shadow)
self.combo = QComboBox()
self.combo.setGraphicsEffect(self.shadow2)
layout.addWidget(self.btn)
layout.addWidget(self.combo)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
docs 声明同一个 QGraphicsEffect 不能被其他小部件共享:
If effect is the installed effect on a different widget, setGraphicsEffect()
will remove the effect from the widget and install it on this widget.
因此您必须创建一个 QGraphicsEffect
for each widget, but if you do not want to write a lot of code and want to apply effects with similar characteristics you could iterate through the widgets and for that you can use findChildren(...)
。
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout(self)
self.btn = QPushButton("Button")
self.combo = QComboBox()
layout.addWidget(self.btn)
layout.addWidget(self.combo)
for child in self.findChildren(QWidget):
shadow = QGraphicsDropShadowEffect(blurRadius=5, xOffset=3, yOffset=3)
child.setGraphicsEffect(shadow)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
子级继承效果,所以您需要做的就是在父级上设置它
import sys
from PySide2.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout()
self.shadow = QGraphicsDropShadowEffect(self)
self.shadow.setBlurRadius(16)
self.shadow.setXOffset(8)
self.shadow.setYOffset(8)
self.btn = QPushButton("Button")
self.combo = QComboBox()
layout.addWidget(self.btn)
layout.addWidget(self.combo)
self.setLayout(layout)
self.setGraphicsEffect(self.shadow)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
我想使用 QGraphicsDropShadowEffect 在几个小部件上设置阴影,我想知道是否有更好的方法可以做到这一点,而不必为我想使用它的每个实例一遍又一遍地编写相同的代码在我下面的例子中。是否可以创建一个 class 或调用的东西,以便我只需要在小部件上设置 setGraphicsEffect()
?我尝试为它创建一些 classes,但我仍然只能让它们创建一个阴影。
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout()
self.shadow = QGraphicsDropShadowEffect()
self.shadow.setBlurRadius(5)
self.shadow.setXOffset(3)
self.shadow.setYOffset(3)
self.shadow2 = QGraphicsDropShadowEffect()
self.shadow2.setBlurRadius(5)
self.shadow2.setXOffset(3)
self.shadow2.setYOffset(3)
self.btn = QPushButton("Button")
self.btn.setGraphicsEffect(self.shadow)
self.combo = QComboBox()
self.combo.setGraphicsEffect(self.shadow2)
layout.addWidget(self.btn)
layout.addWidget(self.combo)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
docs 声明同一个 QGraphicsEffect 不能被其他小部件共享:
If effect is the installed effect on a different widget,
setGraphicsEffect()
will remove the effect from the widget and install it on this widget.
因此您必须创建一个 QGraphicsEffect
for each widget, but if you do not want to write a lot of code and want to apply effects with similar characteristics you could iterate through the widgets and for that you can use findChildren(...)
。
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout(self)
self.btn = QPushButton("Button")
self.combo = QComboBox()
layout.addWidget(self.btn)
layout.addWidget(self.combo)
for child in self.findChildren(QWidget):
shadow = QGraphicsDropShadowEffect(blurRadius=5, xOffset=3, yOffset=3)
child.setGraphicsEffect(shadow)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
子级继承效果,所以您需要做的就是在父级上设置它
import sys
from PySide2.QtWidgets import QWidget, QHBoxLayout, \
QGraphicsDropShadowEffect, QPushButton, QApplication, QComboBox
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
layout = QHBoxLayout()
self.shadow = QGraphicsDropShadowEffect(self)
self.shadow.setBlurRadius(16)
self.shadow.setXOffset(8)
self.shadow.setYOffset(8)
self.btn = QPushButton("Button")
self.combo = QComboBox()
layout.addWidget(self.btn)
layout.addWidget(self.combo)
self.setLayout(layout)
self.setGraphicsEffect(self.shadow)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()