我用PySide6定义了一个文件系统管理器,主要代码是:
self.model = FileSystemModelImpl()
self.root_parent = self.model.setRootPath(img_dir)
self.sort_proxy_model =MySortFilterProxyModel()
self.sort_proxy_model.setSourceModel(self.model)
source_index = self.model.index(img_dir)
proxy_index = self.sort_proxy_model.mapFromSource(source_index)
self.tree_view = QTreeViewImpl()
self.tree_view.setSortingEnabled(True)
self.tree_view.setModel(self.sort_proxy_model)
self.tree_view.setRootIndex(proxy_index)
self.tree_view.selectionModel().selectionChanged.connect(self.tree_response)
当我选择一个文件时,它会调用tree_response函数,其中的一部分
def tree_response(self, selected: QtCore.QItemSelection, deselected: QtCore.QItemSelection):
self.file_path = self.get_current_path()
其中包含 get_current_path() 函数:
def get_current_path(self):
index = self.tree_view.currentIndex()
#file_path = self.model.filePath(self.sort_proxy_model.mapToSource(index))
file_path = self.model.fileInfo(self.sort_proxy_model.mapToSource(index)).absoluteFilePath()
return file_path
文件路径包含中文。当我在 Mac 中运行这段代码时,一切正常。我可以使用 file_path 做一些后处理,比如通过 opencv 读取图像。 但是,当我在 Window 中运行它时,它失败了。 file_path 错误如:
C:/panda_eye/panda_eye_val/img/黑人@160-黑-女-2248_left_crop.jpg
我在 python 3.10 中都使用 python 3.10。我使用 sys.getfilesystemencoding 来获取系统编码类型,它们都是 utf-8。
=====================更新问题========================== ====
这是最小的例子 在 main.py 中,代码是:
from argparse import ArgumentParser, RawTextHelpFormatter
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '.')))
from PySide6.QtWidgets import (QApplication)
from simple_imageviewer import ImageViewer
from PySide6.QtWidgets import QApplication
import multiprocessing
if __name__ == '__main__':
multiprocessing.freeze_support()
arg_parser = ArgumentParser(description="Image Viewer",
formatter_class=RawTextHelpFormatter)
arg_parser.add_argument('file', type=str, nargs='?', help='Image file')
args = arg_parser.parse_args()
app = QApplication(sys.argv)
image_viewer = ImageViewer()
if args.file and not image_viewer.load_file(args.file):
sys.exit(-1)
image_viewer.show()
sys.exit(app.exec())
simple_imageviewer.py 是
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from PySide6.QtWidgets import (QMainWindow, QGridLayout, QFileSystemModel, QTableView, QWidget)
from PySide6.QtGui import QGuiApplication
import PySide6.QtCore as QtCore
from PySide6.QtCore import Slot, QSortFilterProxyModel
import os
import cv2
img_dir = "C:\\Users\\Moon\\OneDrive\\文档\\工作\\code\\python\\test\\"
class ImageViewer(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
# 自适应屏幕大小
primary_size = QGuiApplication.primaryScreen().availableSize()
screen_height = primary_size.height()
screen_width = primary_size.width()
self.move(screen_width * 0.1, screen_height * 0.1)
self.tree_view_layout = QGridLayout()
self.grid_layout = QGridLayout()
self.model = QFileSystemModel()
self.root_parent = self.model.setRootPath(img_dir)
self.sort_proxy_model =QSortFilterProxyModel()
self.sort_proxy_model.setSourceModel(self.model)
source_index = self.model.index(img_dir)
proxy_index = self.sort_proxy_model.mapFromSource(source_index)
self.tree_view = QTableView()
header = self.tree_view.verticalHeader()
header.setSectionHidden(0, True)
self.tree_view.setSortingEnabled(True)
self.tree_view.setModel(self.sort_proxy_model)
self.tree_view.setRootIndex(proxy_index)
#self.tree_view.setRootIndex(self.model.index(img_dir))
self.tree_view.selectionModel().selectionChanged.connect(self.tree_response)
self.tree_view_layout.addWidget(self.tree_view, 0, 0)
self.grid_layout.addLayout(self.tree_view_layout, 0, 0, 0, 1)
main_widget = QWidget()
main_widget.setLayout(self.grid_layout)
self.setCentralWidget(main_widget)
def get_current_path(self):
index = self.tree_view.currentIndex()
source_index = self.sort_proxy_model.mapToSource(index)
file_info = QtCore.QFileInfo(self.model.filePath(source_index))
file_path = file_info.absoluteFilePath()
return file_path
@Slot()
def tree_response(self, selected: QtCore.QItemSelection, deselected: QtCore.QItemSelection):
file_path = self.get_current_path()
img = cv2.imread(file_path)
print(img.shape)
print(file_path)
和“C:\Users\Moon\OneDrive\文档\工”的文件结构。