Adding QMessageBox System Icon into QDialog

Viewed 292

First of all, I want to use QMessageBox subclass to embed a scroll area layout in between QMessageBox informative text and its button at the bottom. But the scroll area overlaps the icon like this

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)

So I decided to create QDialog instead, but I wonder how can I add QMessageBox.Warning icon (also including QMessagebox title and informative text styling) into QDialog, is it possible?. If not then how could I create distance between QMessagebox Icon and its text from the scroll area layout? (because it seems way simpler solution IMO), ps: I really want to minimize external media for icon since my app only does a simple task so that's why I'm really curious if I could take QMessagebox icon instead or if there's no other way I'll be following whatever it is.

1 Answers

The solution is to remove the QDialogButtonBox, add the QScrollArea and then add the removed 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_()

enter image description here

Related