0

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;}")
2
  • you have to do the setStyleSheet command before creating the QMessageBox!!!!!! Commented Mar 6, 2018 at 22:43
  • I'm only a complete beginner in Python. I am just learning as I go along Commented Mar 7, 2018 at 8:49

1 Answer 1

3

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_())
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. I understand now what you mean. I t has to be set inside the def_init_(self) method.
Yeah, @eyllanesc is right. And here is just example that one can execute and see the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.