ColinKennedy
ColinKennedy

Reputation: 998

PyQt implementing dialog icons

Every OS has their own implementation of a file browser and, in PyQt, using QFileDialog will always show that OS's version.

Some applications even have their own implementations of these windows and have their own back/forward/up button icons for those dialogs. Here are a couple examples:

The Foundry Mari

QFileDialog from the program The Foundry Mari

Autodesk Maya

QFileDialog from the program, Autodesk Maya

Both windows come up from the same code,

from PySide import QtGui
QtGui.QFileDialog.getExistingDirectory(QtGui.QWidget(), "Select Directory"))

I'm assuming this is either because of a stylesheet that's being applied by the software (in this case, Maya or Mari) or some other implementation but the point is, I'm making an application where I'd like to utilize the same method. That way, no matter what application a user calls my GUI in, it'll always kind of look like what they're used to seeing in that particular software.

Upvotes: 2

Views: 2432

Answers (2)

Christopher Adams
Christopher Adams

Reputation: 106

You can browse all available icon resources included with Autodesk Maya using the following 2017/Qt5 compatible code, which generates a basic search/filter dialog:

import maya.app.general.resourceBrowser as resourceBrowser
resBrowser = resourceBrowser.resourceBrowser()
resource_path = resBrowser.run()
print(path)  # SP_ComputerIcon.png

The resulting SP_ComputerIcon.png is the same icon pictured above in your Autodesk Maya dialog shot.

You can use any of the included resources once you know the image resource path as such:

# NOTE: Maya 2017 defaults to PySide2/Qt5, this won't work in 2016 or earlier
from PySide2 import QtGui, QtWidgets

testWindow = QtWidgets.QMainWindow()
testWindow.resize(100,100)

resource_path = 'SP_ComputerIcon.png'  # grabbed using preceding code    
pixmap = QtGui.QPixmap(':/{}'.format(resource_path))
icon = QtGui.QIcon(pixmap)

button = QtWidgets.QToolButton()
button.setIcon(icon)

testWindow.setCentralWidget(button)
testWindow.show()

Credit: original source

Upvotes: 1

Brendan Abel
Brendan Abel

Reputation: 37489

You can get the global style icons (usually inherited from the system, unless it's been changed) using QStyle.standardIcon

style = QApplication.instance().style()
icon = style.standardIcon(QStyle.SP_BrowserReload)

There is also QIcon.fromTheme, but that currently only works on linux.

Upvotes: 3

Related Questions