def CreateFlatButton(action): """ Create a custom flat button and style it so that it will look good on all platforms. """ toolButton = QToolButton() toolButton.setIcon(action.icon()) toolButton.setText(action.text()) toolButton.setAutoRaise(True) toolButton.setIconSize(QSize(32, 32)) toolButton.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) if sys.platform.startswith('darwin'): # Bug for Mac: QToolButtons do not react to setAutoRaise so # can't be made flat when they are not used inside a toolbar. # Setting a custom style sheet with border to none fixes this. # But then it looses all its highlight and pressed visual cues # so some extra styling needs to be done to provide some nice # visual feedback on hover and pressed states. toolButton.setStyleSheet( "QToolButton {" "border: none;" "} " "QToolButton:hover {" "background-color: qradialgradient(cx: 0.5, cy: 0.5," "fx: 0.5, fy: 0.5," "radius: 0.5, " "stop: 0 rgba(255, 255, 255, 100), " "stop: 1 rgba(0, 0, 0, 0));" "}" "QToolButton:pressed {" "background-color: qradialgradient(cx: 0.5, cy: 0.5," "fx: 0.5, fy: 0.5," "radius: 0.5, " "stop: 0 rgba(255, 255, 255, 200), " "stop: 1 rgba(0, 0, 0, 0));" "}") font = QFont() font.setPixelSize(10) toolButton.setFont(font) # Connect the clicked signal to the action trigger def pushed(): toolButton.action.triggered.emit() setattr(toolButton, "pushed", pushed) toolButton.clicked.connect(toolButton.pushed) setattr(toolButton, "action", action) return toolButton
def CreateFlatButton(action): """ Create a custom flat button and style it so that it will look good on all platforms. """ toolButton = QToolButton() toolButton.setIcon(action.icon()) toolButton.setText(action.text()) toolButton.setAutoRaise(True) toolButton.setIconSize(QSize(32, 32)) toolButton.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) if sys.platform.startswith('darwin'): # Bug for Mac: QToolButtons do not react to setAutoRaise so # can't be made flat when they are not used inside a toolbar. # Setting a custom style sheet with border to none fixes this. # But then it looses all its highlight and pressed visual cues # so some extra styling needs to be done to provide some nice # visual feedback on hover and pressed states. toolButton.setStyleSheet("QToolButton {" "border: none;" "} " "QToolButton:hover {" "background-color: qradialgradient(cx: 0.5, cy: 0.5," "fx: 0.5, fy: 0.5," "radius: 0.5, " "stop: 0 rgba(255, 255, 255, 100), " "stop: 1 rgba(0, 0, 0, 0));" "}" "QToolButton:pressed {" "background-color: qradialgradient(cx: 0.5, cy: 0.5," "fx: 0.5, fy: 0.5," "radius: 0.5, " "stop: 0 rgba(255, 255, 255, 200), " "stop: 1 rgba(0, 0, 0, 0));" "}") font = QFont() font.setPixelSize(10) toolButton.setFont(font) # Connect the clicked signal to the action trigger def pushed(): toolButton.action.triggered.emit() setattr(toolButton, "pushed", pushed) toolButton.clicked.connect(toolButton.pushed) setattr(toolButton, "action", action) return toolButton