Home > Software engineering >  How to dim background when using qmessagebox?
How to dim background when using qmessagebox?

Time:09-09

I want to set the background in dim mode, when a QMessagebox is popped up.

Currently, I have tried to use a simple QMesssagebox, but the background shows as normal display, when it pops up.

The image for 1st page is as follow enter image description here

When go to next slide is pushed, it goes to next index as follow enter image description here

When going back to 1st index, the back button is pushed which pops up the messagebox as follow enter image description here

However, the mainwindow seems to have no effect on its focus. Therefore, what would I need to do to make it dimmer than the focused messagebox.

How can I do this? Any suggestions?

EDIT

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.ui = uic.loadUi("message.ui",self)
        self.notification = QMessageBox()
        
        self.ui.next_slide.clicked.connect(self.second_index)

        self.ui.go_back.clicked.connect(self.alert_msg)

        self.show()


    def home(self):
        self.ui.stackedWidget.setCurrentIndex(0)

    def second_index(self):
        self.ui.stackedWidget.setCurrentIndex(1)

    def alert_msg(self):
        self.notification.setWindowTitle("Exiting")
        self.notification.setText("Are you sure, you want to exit")
        self.notification.setIcon(QMessageBox.Critical)
        self.notification.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        self.back = self.notification.exec_()

        if self.back == QMessageBox.Yes:
            self.home()
        else:
            pass
    

if __name__ == "__main__":
    app=QApplication(sys.argv)
    mainwindow=MainWindow()

    app.exec_()

CodePudding user response:

You can create a custom widget that is a direct child of the window that has to be "dimmed", ensure that it always has the same size as that window, and just paint it with the selected color:

class Dimmer(QWidget):
    def __init__(self, parent):
        parent = parent.window()
        super().__init__(parent)
        parent.installEventFilter(self)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.adaptToParent()
        self.show()

    def adaptToParent(self):
        self.setGeometry(self.parent().rect())

    def eventFilter(self, obj, event):
        if event.type() == event.Resize:
            self.adaptToParent()
        return super().eventFilter(obj, event)

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.fillRect(self.rect(), QColor(127, 127, 127, 127))


class MainWindow(QMainWindow):
    # ...
    def alert_msg(self):
        dimmer = Dimmer(self)
        # ...
        self.back = self.notification.exec_()
        dimmer.close()

Note that, unless you plan to reuse the "dim widget", it must be destroyed either by calling close() as done above (see the WA_DeleteOnClose flag) or using deleteLater(). Hiding it will not be enough.

  • Related