python - 将QMessageBox系统图标添加到QDialog中

标签 python python-3.x pyqt5 pyside2

首先,我想使用 QMessageBox 子类在 QMessageBox 信息文本及其底部按钮之间嵌入滚动区域布局。但是滚动区域与图标重叠,如下所示

overlapping UI

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 图标与其文本之间的距离? (因为在我看来,这似乎是更简单的解决方案),ps:我真的想最小化图标的外部媒体,因为我的应用程序只执行一个简单的任务,所以这就是为什么我真的很好奇是否可以使用 QMessagebox 图标,或者如果没有其他方法不管是什么我都会关注。

最佳答案

解决方案是删除QDialogBu​​ttonBox,添加QScrollArea,然后添加删除的QDialogBu​​ttonBox:

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_()

enter image description here

关于python - 将QMessageBox系统图标添加到QDialog中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64583442/

相关文章:

python - 从 python : openmp library not found? 调用 MKL 的线程函数

python - Flask - uploadnotallowed 错误 - 重命名要保存的文件时

python - "pip3 list --outdated"项目在 "pip3 install -U"之后保留剩余

python - 如何解释从 Google OR Tools 返回的车辆路径问题解决方案?

python - Maketrans 不适用于 python3.4 的 petl

python - 命名管道不会阻塞

python - 如何确定 PyQt5 对话框是否会显示在屏幕外

python - 在 Qt 5 中嵌入 Python3

python - 如何在不影响 Pyqt5 中的小部件的情况下将背景图像添加到主窗口

python - PyQt5可拖动无框窗口