如果父窗口小部件也有效,则在添加QGraphicsDropShadow效果时,QPushButton将变为不可见

时间:2017-07-18 16:11:59

标签: python python-3.x pyqt qt5 pyqt5

我正在使用QFrame使用QGraphicsDropShadowEffect制作带阴影的“卡片”。我遇到的问题是,如果我在卡上添加一个按钮,然后在按钮上添加一个投影,该按钮是不可见的,但仍然可以点击。如果我从卡上移除阴影,它会显示按钮正常,或者如果我从按钮上移除阴影,它就会显示。似乎我不能在卡片和按钮上留下阴影。

我的课程:

class Card(QFrame):
    """ """
    def __init__(self, title='Card Title', cls_layout=QVBoxLayout,
                 vsizing=QSizePolicy.Preferred, hsizing=QSizePolicy.Preferred,
                 has_shadow=False, subtitle='', parent=None):
        super().__init__(parent)

        self.has_shadow = has_shadow
        self.setStyleSheet('QFrame { border-radius: 2px; background: white;}')
        self._layout = QVBoxLayout(self)
        self._layout.setAlignment(Qt.AlignTop)
        self._lblTitle = QLabel(title)
        self._lblTitle.setFont(make_font(pt=12, b=True))
        self._lblSubtitle = QLabel()
        self._lblSubtitle.setIndent(4)
        self._layout.addWidget(self._lblTitle)
        self._layout.addWidget(self._lblSubtitle)
        self._layout.addSpacing(4)
        #self.subtitle = subtitle # property not used for example
        self.enabled = True

        self.layout = cls_layout()  # where content is added
        self._layout.addLayout(self.layout)
        self.setSizePolicy(hsizing, vsizing)

    @property
    def enabled(self):
        return self.isEnabled()

    @enabled.setter
    def enabled(self, state):
        if state and self.has_shadow:
            effect = QGraphicsDropShadowEffect(self)
            effect.setOffset(1, 2)
            effect.setBlurRadius(4)
            self.setGraphicsEffect(effect)
        else:
            self.setGraphicsEffect(None)

        self.setEnabled(state)

class PushButton(QPushButton):
    def __init__(self, text, width=75, height=30, parent=None):
        super().__init__(text, parent)
        self.setFixedWidth(width)
        self.setFixedHeight(height)
        self.enabled = True

    @property
    def enabled(self):
        return self.isEnabled()

    @enabled.setter
    def enabled(self, state):
        self.setGraphicsEffect(None)
        if state:
            self.effect = QGraphicsDropShadowEffect(self)
            self.effect.setOffset(1, 2)
            self.effect.setBlurRadius(4)
            self.setGraphicsEffect(self.effect)

        self.setEnabled(state)

这是我如何使用它们的一个例子:

from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
import sys


if __name__ == '__main__':

    app = QApplication([])

    window = QMainWindow()
    widget = QWidget()
    layout = QVBoxLayout(widget)
    window.setCentralWidget(widget)

    card = Card(has_shadow=True)
    btn = PushButton('Test Button')
    btn.clicked.connect(print)
    card.layout.addWidget(btn)
    btn.enabled = False  # Button Visible
    # btn.enabled = True   # Button Invisible, but still click-able

    layout.addWidget(card)

    window.show()
    sys.exit(app.exec_())

我尝试放弃卡片上的阴影并将QFrame设置为带有凸起阴影的StyledPanel,但即使我从中移除了css,我也无法显示它们。我正在使用带有Anaconda 4.4的Windows 7,如果这有所不同的话。我假设我使用图形效果错误或做错其他的事情,但我找不到任何其他类似问题的帖子。

1 个答案:

答案 0 :(得分:0)

我最终使用this方法并将我的按钮类更改为:

class Button(QWidget):
    clicked = pyqtSignal()

    def __init__(self, text, w=75, h=50, parent=None):
        super().__init__(parent)
        layout = QVBoxLayout(self)
        layout.setContentsMargins(QMargins(0, 0, 0, 0))
        self.setFixedSize(w + 5, h + 6)
        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        self.scene = QtWidgets.QGraphicsScene(self)
        self.view = QtWidgets.QGraphicsView(self)
        self.view.setScene(self.scene)
        self._btn = QPushButton(text)
        self._btn.setFixedSize(w, h)
        self._btn.setAttribute(Qt.WA_TranslucentBackground)
        self._btn.setStyleSheet(gui.css.button_css())
        self._btn.clicked.connect(self.click)
        self.btn = self.scene.addWidget(self._btn)
        self.enabled = True

        layout.addWidget(self.view)

    @property
    def enabled(self):
        return self._btn.isEnabled()

    @enabled.setter
    def enabled(self, state):
        self._btn.setEnabled(state)

        if state:
            shadow = QGraphicsDropShadowEffect(self)
            shadow.setOffset(1, 2)
            shadow.setBlurRadius(4)
            self.btn.setGraphicsEffect(shadow)
        else:
            self.btn.setGraphicsEffect(None)

    def click(self):
        self.clicked.emit()