I am trying to implement dialog similar to error message box with additional functionality. I would like to draw the same bitmap as is provided by system when using QMessageBox.Critical in QMessageBox.

In wxPython I would do this:

self.error_bitmap = wx.ArtProvider.GetBitmap(wx.ART_ERROR, wx.ART_MESSAGE_BOX)
self.error_bitmap_ctrl = wx.StaticBitmap(self)
self.error_bitmap_ctrl.SetBitmap(self.error_bitmap)

I was looking for something similar in Qt. I tried to use QStyle.SP_MessageBoxCritical or QIcon.fromTheme("dialog-error"), but without success. It seems I do not understand the class structure to get some widget I would be actually able to put on window next to QLabel.

有帮助吗?

解决方案

The various built-in icons used by Qt can be retrieved via the QStyle.standardIcon method.

The QMessageBox class also has a method to extract the pixmap for each QMessageBox.Icon, but it's not part of the public API. Here's a PyQt/PySide port of it:

def messageBoxIcon(mbicon, widget=None):
    if widget is not None:
        style = widget.style()
    else:
        style = QtGui.QApplication.style()
    size = style.pixelMetric(
        QtGui.QStyle.PM_MessageBoxIconSize, None, widget)
    if mbicon == QtGui.QMessageBox.Information:
        icon = style.standardIcon(
            QtGui.QStyle.SP_MessageBoxInformation, None, widget)
    elif mbicon == QtGui.QMessageBox.Warning:
        icon = style.standardIcon(
            QtGui.QStyle.SP_MessageBoxWarning, None, widget)
    elif mbicon == QtGui.QMessageBox.Critical:
        icon = style.standardIcon(
            QtGui.QStyle.SP_MessageBoxCritical, None, widget)
    elif mbicon == QtGui.QMessageBox.Question:
        icon = style.standardIcon(
            QtGui.QStyle.SP_MessageBoxQuestion, None, widget)
    else:
        icon = QtGui.QIcon()
    if not icon.isNull():
        return icon.pixmap(size, size)
    return QtGui.QPixmap()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top