我是QT的新手,这可能非常基础,但是我无法在自己的消息框中使用样式表。
我现在的代码来自:
self.actionLoad_Portfolio_Settings.triggered.connect(self.openPortfolioSettings)
是:
def openPortfolioSettings(self):
# some code
QMessageBox.about(self, "Portfolio settings","Portfolio settings loaded successfullly")
QMessageBox.setStyleSheet(self,".QMessageBox.about {border: 20px solid black;border-radius: 10px;background-color: rgb(1, 1, 1);}")
但这不会执行任何操作,消息框会像以前一样显示。
最终我要做的是为所有对话框,寡妇和主窗口设置样式表,这样我就可以使用类似的东西了:
my_stylesheet=foo
somewindow.setStyleSheet(my)stylesheet)
非常感谢任何帮助。
答案 0 :(得分:2)
QMessageBox.about()是静态方法,因此您无法直接获取对象来设置样式表,因此,根据您拥有的其他代码以及是否要将相同的样式应用于所有QMessageBox,有几种策略。
如果您只想应用到生成about方法的QMessageBox上,则可以进行过滤,在这种情况下,我将假定它是唯一可见的QMessageBox,然后使用QTimer.singleShot(0, ...)
来获取小部件通过findChildren()
方法通过其父部件:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
settings_menu = self.menuBar().addMenu("Settings")
self.actionLoad_Portfolio_Settings = settings_menu.addAction(
"Load Portafolio"
)
self.actionLoad_Portfolio_Settings.triggered.connect(
self.openPortfolioSettings
)
@QtCore.pyqtSlot()
def openPortfolioSettings(self):
QtCore.QTimer.singleShot(0, self.after_about_portafolio_settings)
QtWidgets.QMessageBox.about(
self,
"Portfolio settings",
"Portfolio settings loaded successfullly",
)
@QtCore.pyqtSlot()
def after_about_portafolio_settings(self):
for messagebox in self.findChildren(QtWidgets.QMessageBox):
if messagebox.isVisible():
messagebox.setStyleSheet(
"""QMessageBox{border: 20px solid black;border-radius: 10px;background-color: rgb(1, 1, 1);}"""
)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
相反,如果您想对窗口中的所有QMessageBox子代应用相同的样式,则将该样式放置在窗口的样式表中:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setStyleSheet(
"""
/* another styles*/
QMessageBox{border: 20px solid black;border-radius: 10px;background-color: rgb(1, 1, 1);}
"""
)
settings_menu = self.menuBar().addMenu("Settings")
self.actionLoad_Portfolio_Settings = settings_menu.addAction(
"Load Portafolio"
)
self.actionLoad_Portfolio_Settings.triggered.connect(
self.openPortfolioSettings
)
@QtCore.pyqtSlot()
def openPortfolioSettings(self):
QtWidgets.QMessageBox.about(
self,
"Portfolio settings",
"Portfolio settings loaded successfullly",
)
如果相反,您想对整个应用程序中的所有QMessageBox应用相同的样式,则在QApplication中设置样式表:
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(
"""
/* another styles*/
QMessageBox{border: 20px solid black;border-radius: 10px;background-color: rgb(1, 1, 1);}
"""
)
另一种解决方案是不使用QMessage.about()方法,而是创建QMessageBox对象:
def openPortfolioSettings(self):
messagebox = QtWidgets.QMessageBox(
QtWidgets.QMessageBox.NoIcon,
"Portfolio settings",
"Portfolio settings loaded successfullly",
parent=self,
)
messagebox.setAttribute(QtCore.Qt.WA_DeleteOnClose)
messagebox.setStyleSheet(
"""
QMessageBox{border: 20px solid black;border-radius: 10px;background-color: rgb(1, 1, 1);}
"""
)
messagebox.exec_()