1
我正在使用QFrame使用QGraphicsDropShadowEffect製作帶有陰影的「卡片」。我遇到的問題是,如果向卡添加按鈕,然後將按鈕添加到按鈕,該按鈕不可見但仍可點擊。如果我從卡上取下陰影,它會顯示按鈕正常,或者如果我從按鈕上移除了陰影,它就會顯示出來。看來我不能在卡片和按鈕上放置陰影。QPushButton在添加QGraphicsDropShadow效果時變得不可見,如果父窗口小部件也有效果
我的課:
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。我假設我要麼使用圖形效果錯誤,要麼做不正確的事情,但我一直沒能找到任何其他類似問題的帖子。