當彈出 QMessagebox 時,我想將背景設定為暗模式。
目前,我嘗試使用一個簡單的 QMesssagebox,但是當它彈出時,背景顯示為正常顯示。
第一頁的圖片如下

當go to next slide被推送時,它會進入下一個索引,如下所示

回傳到第一個索引時,按下后退按鈕,彈出訊息框如下

但是,主視窗似乎對其焦點沒有影響。因此,我需要做些什么才能使它比聚焦的訊息框更暗。
我怎樣才能做到這一點?有什么建議么?
編輯
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_()
uj5u.com熱心網友回復:
您可以創建一個自定義小部件,它是必須“變暗”的視窗的直接子視窗,確保它始終具有與該視窗相同的大小,并使用選定的顏色對其進行繪制:
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()
請注意,除非您打算重用“dim 小部件”,否則必須通過close()按上述方式呼叫(參見WA_DeleteOnClose標志)或使用deleteLater(). 隱藏它是不夠的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/506292.html
