GreenCoder90
GreenCoder90

Reputation: 363

Setting background of QPushButton in QMessageBox

I have a class that displays a QMessageBox each time an action is performed. I was trying to set the button colour in the QMessageBox to a silver background.

At the moment the button is blue which is the same as the background of the QMessageBox.

My question is, how, with this piece of code: QtWidgets.qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}") can i change the QPushButton colour in the QMessageBox to silver.

This is a snippet of my code. I have tried to put the above snippet into the function so that when the button is clicked, the colour of the QPushButton in the message box will be silver. Is there a problem with this as it does not seem to make any change. Where should I place this styleSheet functionality in the code?

self.canonicalAddressesButton.clicked.connect(self.canonical_data_parsed_notification)

def canonical_data_parsed_notification(self):
        QtWidgets.QMessageBox.information(self.mainwindow, 'Notification', 'Canonical Address Data Has Been Parsed!', QtWidgets.QMessageBox.Ok) 
QtWidgets.qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")

Upvotes: 0

Views: 2126

Answers (1)

peremeykin
peremeykin

Reputation: 549

The setStyleSheet() method should be invoked before creating the QMessageBox. Here is the trivial example how you can do it:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, qApp, QMessageBox

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(0, 0, 300, 200)
        button = QPushButton('Click me', self)
        qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
        button.clicked.connect(self.button_clicked)

    def button_clicked(self):
        QMessageBox.information(self, 'Notification', 'Text', QMessageBox.Ok) 


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = App()
    widget.show()
    sys.exit(app.exec_())

Upvotes: 3

Related Questions