将 QMessageBox 系统图标添加到 QDialog
Adding QMessageBox System Icon into QDialog
首先,我想使用QMessageBox 子类在QMessageBox 信息文本和底部按钮之间嵌入一个滚动区域布局。但是滚动区域覆盖ps这样的图标
class CustomizedMessagebox(QMessageBox):
def __init__(self,parent,dic):
QMessageBox.__init__(self,parent)
self.setIcon(QMessageBox.Warning)
self.setText("Sample Warning Text Here")
scroll = QScrollArea(self)
scroll.setWidgetResizable(True)
self.content = QWidget()
scroll.setWidget(self.content)
lay = QVBoxLayout(self.content)
for item in [[1,2],[3,4],[5,6],[7,8],[9,0]]: #just for scroll able example list
lay.addWidget(QLabel("{} - {}".format(item[0],item[1]), self))
self.layout().addWidget(scroll, 1, 0, 1, self.layout().columnCount())
#self.setStyleSheet("QScrollArea{min-width:200 px; min-height: 200px}") #style that i want to add later on
self.addButton('Understood',self.AcceptRole)
所以我决定改为创建 QDialog,但我想知道如何将 QMessageBox.Warning 图标(还包括 QMessagebox 标题和信息文本样式)添加到 QDialog 中,这可能吗?如果不是那么我怎么能在 QMessagebox 图标和滚动区域布局中的文本之间创建距离? (因为 IMO 似乎更简单的解决方案),ps:我真的想尽量减少图标的外部媒体,因为我的应用程序只执行一个简单的任务,所以这就是为什么我真的很好奇我是否可以使用 QMessagebox 图标,或者如果没有别的办法,我会一直关注它的。
解决方法是去掉QDialogButtonBox,添加QScrollArea,然后添加去掉的QDialogButtonBox:
from PySide2.QtWidgets import (
QApplication,
QDialogButtonBox,
QLabel,
QMessageBox,
QScrollArea,
QVBoxLayout,
QWidget,
)
class CustomizedMessagebox(QMessageBox):
def __init__(self, parent=None):
QMessageBox.__init__(self, parent)
self.setIcon(QMessageBox.Warning)
self.setText("Sample Warning Text Here")
self.addButton("Understood", QMessageBox.AcceptRole)
scroll = QScrollArea(widgetResizable=True)
self.content = QWidget()
scroll.setWidget(self.content)
box = self.findChild(QDialogButtonBox)
self.layout().removeWidget(box)
self.layout().addWidget(
scroll, self.layout().rowCount(), 0, 1, self.layout().columnCount()
)
self.layout().addWidget(
box, self.layout().rowCount(), 0, 1, self.layout().columnCount()
)
lay = QVBoxLayout(self.content)
for item in [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]:
lay.addWidget(QLabel("{} - {}".format(item[0], item[1]), self))
app = QApplication([])
w = CustomizedMessagebox()
w.exec_()
首先,我想使用QMessageBox 子类在QMessageBox 信息文本和底部按钮之间嵌入一个滚动区域布局。但是滚动区域覆盖ps这样的图标
class CustomizedMessagebox(QMessageBox):
def __init__(self,parent,dic):
QMessageBox.__init__(self,parent)
self.setIcon(QMessageBox.Warning)
self.setText("Sample Warning Text Here")
scroll = QScrollArea(self)
scroll.setWidgetResizable(True)
self.content = QWidget()
scroll.setWidget(self.content)
lay = QVBoxLayout(self.content)
for item in [[1,2],[3,4],[5,6],[7,8],[9,0]]: #just for scroll able example list
lay.addWidget(QLabel("{} - {}".format(item[0],item[1]), self))
self.layout().addWidget(scroll, 1, 0, 1, self.layout().columnCount())
#self.setStyleSheet("QScrollArea{min-width:200 px; min-height: 200px}") #style that i want to add later on
self.addButton('Understood',self.AcceptRole)
所以我决定改为创建 QDialog,但我想知道如何将 QMessageBox.Warning 图标(还包括 QMessagebox 标题和信息文本样式)添加到 QDialog 中,这可能吗?如果不是那么我怎么能在 QMessagebox 图标和滚动区域布局中的文本之间创建距离? (因为 IMO 似乎更简单的解决方案),ps:我真的想尽量减少图标的外部媒体,因为我的应用程序只执行一个简单的任务,所以这就是为什么我真的很好奇我是否可以使用 QMessagebox 图标,或者如果没有别的办法,我会一直关注它的。
解决方法是去掉QDialogButtonBox,添加QScrollArea,然后添加去掉的QDialogButtonBox:
from PySide2.QtWidgets import (
QApplication,
QDialogButtonBox,
QLabel,
QMessageBox,
QScrollArea,
QVBoxLayout,
QWidget,
)
class CustomizedMessagebox(QMessageBox):
def __init__(self, parent=None):
QMessageBox.__init__(self, parent)
self.setIcon(QMessageBox.Warning)
self.setText("Sample Warning Text Here")
self.addButton("Understood", QMessageBox.AcceptRole)
scroll = QScrollArea(widgetResizable=True)
self.content = QWidget()
scroll.setWidget(self.content)
box = self.findChild(QDialogButtonBox)
self.layout().removeWidget(box)
self.layout().addWidget(
scroll, self.layout().rowCount(), 0, 1, self.layout().columnCount()
)
self.layout().addWidget(
box, self.layout().rowCount(), 0, 1, self.layout().columnCount()
)
lay = QVBoxLayout(self.content)
for item in [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]:
lay.addWidget(QLabel("{} - {}".format(item[0], item[1]), self))
app = QApplication([])
w = CustomizedMessagebox()
w.exec_()