@@ -182,10 +182,24 @@ def get_image_file_path(name: str, default: str = "not_found.png") -> str:
182182ICON_CACHE = {}
183183
184184
185+ def get_std_icon (name : str ) -> QG .QIcon | None :
186+ """Get standard icon by name"""
187+ # Importing Qt objects here because this module should not depend on them
188+ # pylint: disable=import-outside-toplevel
189+
190+ # Try to get standard icon first
191+ from guidata .qthelpers import get_std_icon
192+
193+ try :
194+ return get_std_icon (name )
195+ except AttributeError :
196+ return None
197+
198+
185199def get_icon (name : str , default : str = "not_found.png" ) -> QG .QIcon :
186200 """
187201 Construct a QIcon from the file with specified name
188- name, default: filenames with extensions
202+ name, default: filenames with extensions or standard Qt icon names
189203
190204 Args:
191205 name (str): name of the icon
@@ -197,22 +211,25 @@ def get_icon(name: str, default: str = "not_found.png") -> QG.QIcon:
197211 try :
198212 return ICON_CACHE [name ]
199213 except KeyError :
200- # Importing Qt objects here because this module should not depend on them
201- # pylint: disable=import-outside-toplevel
214+ std_icon = get_std_icon (name )
215+ if std_icon is not None :
216+ return std_icon
202217
203- # Try to get standard icon first
204- from guidata .qthelpers import get_std_icon
218+ std_default_icon = get_std_icon (default )
205219
206- try :
207- return get_std_icon (name )
208- except AttributeError :
209- pass
220+ # Importing Qt objects here because this module should not depend on them
221+ # pylint: disable=import-outside-toplevel
210222
211223 # Retrieve icon from file (original implementation)
212224 from qtpy import QtGui as QG
213225
214- icon = QG .QIcon (get_image_file_path (name , default ))
215- ICON_CACHE [name ] = icon
226+ try :
227+ icon = QG .QIcon (get_image_file_path (name , default ))
228+ ICON_CACHE [name ] = icon
229+ except RuntimeError : # default is a standard icon name
230+ if std_default_icon is not None :
231+ return std_default_icon
232+ raise
216233 return icon
217234
218235
0 commit comments