PyQt实现对话框图标

时间:2016-10-18 14:16:58

标签: pyqt pyside maya

每个操作系统都有自己的文件浏览器实现,在PyQt中,使用QFileDialog将始终显示操作系统的版本。

某些应用程序甚至拥有自己的这些窗口的实现,并为这些对话框提供了自己的后退/前进/上移按钮图标。以下是几个例子:

The Foundry Mari

QFileDialog from the program The Foundry Mari

Autodesk Maya

QFileDialog from the program, Autodesk Maya

两个窗口都来自相同的代码,

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

我假设这是因为软件应用的样式表(在这种情况下,Maya或Mari)或其他一些实现,但重点是,我正在制作我希望使用相同方法的应用程序。这样,无论用户在哪个应用程序中调用我的GUI,它总是看起来像他们在该特定软件中看到的那样。

2 个答案:

答案 0 :(得分:2)

您可以使用QStyle.standardIcon

获取全局样式图标(通常从系统继承,除非它已被更改)
style = QApplication.instance().style()
icon = style.standardIcon(QStyle.SP_BrowserReload)

还有QIcon.fromTheme,但目前只适用于linux。

答案 1 :(得分:1)

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