gpt4 book ai didi

c++ - 无论文件如何,QFileIconProvider 都返回相同的图标

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:43 25 4
gpt4 key购买 nike

我正在使用 PySide(包装 Qt 4.8 框架)开发应用程序。我需要显示与某些文件扩展名关联的应用程序图标,为此我使用了 QFileIconProvider。在 Windows 上,我的代码运行完美——每个文件扩展名都与相应应用程序的图标一起显示。但是,在 Linux Ubuntu (14.04.1) 上,相同的代码会为我尝试的所有文件扩展名显示未知文件扩展名的图标。

有人知道为什么会这样吗?这是代码:

from PySide import QtCore, QtGui

# Use the appropriate path module when running on Windows.
path = None
import os
if os.name == "nt":
path = __import__("ntpath")
else:
path = os.path


class MyWidget(QtGui.QWidget):
def __init__(self, *args, **kwargs):
# ...
# ...
self.file_ext_to_icon = {}

def get_icon(self, fpath):
_, file_ext = path.splitext(fpath)
file_ext = file_ext.replace(".", "")
if file_ext in self.file_ext_to_icon:
return self.file_ext_to_icon[file_ext]
if path.exists(fpath):
icon = QtGui.QFileIconProvider().icon(QtCore.QFileInfo(fpath))
else:
temp_fpath = path.join(os.getcwd(), "myappname_temp.%s" % file_ext)
if not path.exists(temp_fpath):
with open(temp_fpath, "wb") as _:
pass
icon = QtGui.QFileIconProvider().icon(QtCore.QFileInfo(temp_fpath))
os.remove(temp_fpath)
if icon.isNull():
# Use a custom default file icon from the resources file. Note that
# this is different from the default file icon given by the OS.
icon = QtGui.QIcon(":images/default_file_icon.png")
self.file_ext_to_icon[file_ext] = icon
return icon

最佳答案

我遇到了同样的问题,在这里进行了大量挖掘之后,我最终做了...

class IconProvider(QtWidgets.QFileIconProvider):
def __init__(self):
super().__init__()
self.mimeDatabase = QtCore.QMimeDatabase()

def icon(self, info: QtCore.QFileInfo):
mimeType = self.mimeDatabase.mimeTypeForFile(info)
return QtGui.QIcon.fromTheme(mimeType.iconName())


...used in my class
self.filesModel = QtWidgets.QFileSystemModel(self.filesView)
self.filesModel.setIconProvider(IconProvider())


...or in your case
icon = IconProvider().icon(QtCore.QFileInfo(fpath))

关于c++ - 无论文件如何,QFileIconProvider 都返回相同的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28603584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com