在 PyQt5 中向所有边添加框阴影

Adding box shadow to all sides in PyQt5

所以,我有一个 QWidget 和一个虚拟文本。所以,我希望这个标签相当高。所以,我选择 QGraphicsDropShadowEffect 并添加为图形效果。但是,我只能在底部和右侧的两侧看到盒子阴影。我也想给另外两个边加上阴影

这是我目前编写的代码:

import sys
from PyQt5.QtWidgets import QApplication, QGraphicsDropShadowEffect, QWidget, QLabel
from PyQt5.QtCore import QRect, Qt


def window():
    app = QApplication(sys.argv)

    widget = QWidget()

    textLabel = QLabel(widget)

    textLabel.setText("Hello World!")

    textLabel.setStyleSheet(
        "background-color: #4C566A; color: #88C0D0; font-size: 30px; font-weight: bold"
    )

    textLabel.setAlignment(Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignCenter)

    textLabel.setGeometry(QRect(100, 100, 500, 500))

    effect = QGraphicsDropShadowEffect()

    effect.setBlurRadius(15)

    textLabel.setGraphicsEffect(effect)

    textLabel.move(110, 85)

    widget.setGeometry(50, 50, 320, 200)

    widget.setStyleSheet("background-color: #2E3440")

    widget.setWindowTitle("PyQt5 Example")

    widget.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    window()

正如@musicamante 所说,您应该将偏移量设置为 (0 , 0)

完整答案如下:

import sys
from PyQt5.QtWidgets import QApplication, QGraphicsDropShadowEffect, QWidget, QLabel
from PyQt5.QtCore import QRect, Qt


def window():
    app = QApplication(sys.argv)

    widget = QWidget()

    textLabel = QLabel(widget)

    textLabel.setText("Hello World!")

    textLabel.setStyleSheet(
        "background-color: #4C566A; color: #88C0D0; font-size: 30px; font-weight: bold"
    )

    textLabel.setAlignment(Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignCenter)

    textLabel.setGeometry(QRect(100, 100, 500, 500))

    effect = QGraphicsDropShadowEffect()

    effect.setOffset(0, 0)

    effect.setBlurRadius(15)

    textLabel.setGraphicsEffect(effect)

    textLabel.move(110, 85)

    widget.setGeometry(50, 50, 320, 200)

    widget.setStyleSheet("background-color: #2E3440")

    widget.setWindowTitle("PyQt5 Example")

    widget.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    window()