Qt UIC: Non-modal pop-up not inheriting style from parent

270 Views Asked by At

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).

0

There are 0 best solutions below