0

I am trying to create a non-modal (modeless) QMessageBox window with several buttons and text. To make it modeless, I am giving it the MainWindow object as its parent (which inherits from QMainWindow).

For example:

class MainWindow(QMainWindow):
    ...

    def create_popup(self):
        message_box = QMessageBox(self)
        # add buttons, text, etc.

        message_box.setModal(False)
        message_box.show()

However, the issue arises that I am providing the styling of my MainWindow through a stylesheet, built from a .ui file (using PyQt5.uic.loadUi('file', main_window)). In the .ui file, I am specifying the stylesheet of the MainWindow as having background-color: black. Because the QMessageBox I create inherits from MainWindow, it also inherits its stylesheet, making both the background of the box black and that of the buttons. With inheritance, modeless

I know I could manually style the box, but I would like to be able the remove the style applied by the parent to the children, while still maintaining the ability to use the QMessageBox as a modeless pop-up.

If I remove the parent of the QMessageBox (so I do message_box = QMessageBox(None)), calling show() does nothing - I have to use exec_(). This does result in the expected presentation of the box with the default styling, however. Without inheritance, modal

If I try to manually override the background-color attribute (with message_box.setStyleSheet("background-color: grey;")), this messes up the style of the buttons, and makes them into rectangular boxes (since the buttons also inherit from the QMessageBox). With custom stylesheet

I would like to make it so that either 1) the style applied to my MainWindow is not inherited by the QMessageBox, 2) the QMessageBox can be modeless without being a child of the MainWindow, or 3) I can override the styles with the full styles that are default to my platform (including OS and dark mode preferences, like Qt normally does).

WhyIsItReal
  • 81
  • 3
  • 14
  • Note: adding the `self` argument to a widget has nothing to do with inheritance, which is about the *class*; that argument is to declare the *parenthood* of the object. That said, setting a generic property for a whole widget is generally a bad idea, as that property will be inherited by ***all*** child widgets, with unexpected (and unwanted) results: try to add a scroll area and you'll see. Please provide the *full* stylesheet you're using for the main window. – musicamante Dec 02 '21 at 23:14
  • The stylesheet of the `MainWindow` (from Qt Creator) is `background-color: rgb(0, 0, 0);`. How do you recommend I set the background color without using a stylesheet? – WhyIsItReal Dec 03 '21 at 01:00
  • Setting generic stylesheet properties is normally discouraged for objects that can have children (see the above comment). A possibility is to set the palette of the application (or main widget): `palette = self.palette()` `palette.setColor(palette.Window, Qt.black)` `self.setPalette(palette)` (you can do the same on Designer by setting the palette of the top level widget). But, what's not clear to me is how you actually want your message box to be displayed: should it inherit the background of the main window? or should it use the default style of the OS? – musicamante Dec 03 '21 at 01:28
  • I want it to use the default style of the OS, as if it had no style applied to it at all (like how it looks for the modal no-parent version). – WhyIsItReal Dec 03 '21 at 02:00
  • 1
    A possible alternative could be to set the stylesheet for the central widget (and its children) only; assuming the *Qt* object name (what's set in Designer) of the central widget is `centralwidget`: `#centralwidget, #centralwidget * { background: black; }` (but, as said, you should ***not*** use generic properties for all widgets). In this way, the dialog will not inherit the stylesheet. Note that your attempt with `show()` didn't work because you're not creating a persistent reference for the dialog, so it gets instantly garbage collected; change to `self.message_box = ...` and it'll work. – musicamante Dec 03 '21 at 14:28

0 Answers0