how to use default icons with qt

2019-07-05 05:47发布

问题:

I'm using only Qt (though PyQt) and I don't get icons when I call e.g. QMessageBox.warning(). Is there a way to use the platform's default icons? Currently I'm using the more complete QMessageBox constructor and calling setIconPixmap.

回答1:

http://doc.qt.io/qt-5/qmessagebox.html

from the above link:

QMessageBox supports four predefined message severity levels, or message types, which really only differ in the predefined icon they each show. Specify one of the four predefined message types by setting the icon property to one of the predefined Icons.

so you should use the QMessageBox constructor:

QMessageBox ( Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint )

and you can use one of these icons:

http://doc.qt.io/qt-5/qmessagebox.html#Icon-enum

for example you can pass this parameter for icon: QMessageBox::Warning

HTH! greets



回答2:

I'm not too familiar with using Qt via Python, or using QMessageBox. However, a quick glance at the documentation implies that a warning would use the warning icon, which should be the platform's default icons if they haven't been changed. Have you tried looking through Qt's bug tracking system to see if it is a bug? Or submitting a bug/feature request for it?



回答3:

I am not sure how you have written your usage of the message box but here is how I use it:

from PyQt4 import QtGui
QtGui.QMessageBox.warning(parent, dialogTitle, dialogText)

If that does not produce a warning icon in your message box, then you may want to consider upgrading your PyQt to the latest version, because I have experienced small bugs like this in the past with some PyQt revisions.



标签: qt qt4 pyqt pyqt4 kde