-1

I have a below code for QMessageBox:

msgBox = QMessageBox()
reply = msgBox.question(self, 'Window Close', 'Are you sure you want to close the window?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

This is showing as below:

enter image description here

So I added below line of code:

msgBox.setStyleSheet("QLabel{ color: white}")

But looks like its not working as its still the same black color. How can I change the color to while.

Below is the full code:

UI Code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'app.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(790, 480)
        MainWindow.setStyleSheet("background-color: rgb(16, 16, 16);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 81, 31))
        self.pushButton.setStyleSheet("background-color: rgb(255, 255, 255);\n"
                                      "color: rgb(0, 0, 0);")
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 70, 601, 341))
        self.textEdit.setStyleSheet("background-color: rgb(255, 255, 255);\n"
                                    "color: rgb(0, 0, 0);")
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 790, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

app.py code:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from src.app_ui import Ui_MainWindow


class HomeWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def closeEvent(self, event):
        msgBox = QMessageBox()
        msgBox.setStyleSheet("QLabel{ color: white}")
        reply = msgBox.question(self, 'Window Close', 'Are you sure you want to close the window?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


app = QApplication(sys.argv)
main_window = HomeWindow()
main_window.show()
sys.exit(app.exec_())

Expected output:

enter image description here

S Andrew
  • 5,592
  • 27
  • 115
  • 237

1 Answers1

1

QMessageBox::question() is a static method that creates a QMessageBox object, displays it and returns the choice that is different from the msgBox object where you set the stylesheet. A possible solution is to not use the QMessageBox::question() method and build the window using msgBox:

def closeEvent(self, event):
    msgBox = QMessageBox(
        QMessageBox.Question,
        "window Close",
        "Are you sure you want to close the window?",
        buttons=QMessageBox.Yes | QMessageBox.No,
        parent=self,
    )
    msgBox.setDefaultButton(QMessageBox.No)
    msgBox.setStyleSheet("QLabel{ color: white}")
    msgBox.exec_()
    reply = msgBox.standardButton(msgBox.clickedButton())
    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

The Qt stylesheet spreads cascading, from parent to child, which is why this style is observed. If it is not desired then a possible option is not to pass it a parent:

reply = msgBox.question(
    None,
    "Window Close",
    "Are you sure you want to close the window?",
    QMessageBox.Yes | QMessageBox.No,
    QMessageBox.No,
)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you but I think using Qlabel it only changes the color of the text but how can I change the color of the overall message box to white – S Andrew Sep 11 '20 at 03:26
  • @SAndrew I do not understand what you mean. Could you explain what you want to get, maybe a picture of what you want would help. – eyllanesc Sep 11 '20 at 03:29
  • I have added a screenshot of the expected message box. – S Andrew Sep 11 '20 at 03:58
  • Okay I just removed the `parent=self` from your code and its now showing as expected. Looks like setting `parent=self` gets the base color of the parent window. – S Andrew Sep 11 '20 at 04:00
  • @SAndrew Exactly, one of the consequences of setting a parent is that the stylessheet spreads from parent to child. – eyllanesc Sep 11 '20 at 04:02
  • @eyllanesc Is it possible to replace the ugly logo in the title bar or maybe just remove it entirely? – Voldemort Apr 21 '22 at 12:34