我想实现以下目标:
我可以在多个驱动器中选择多个文件夹并检索所选文件夹的文件夹路径。Qt 仅具有粗略的多文件夹选择功能,但不支持从其他驱动器等中选择文件夹。
谁能指导我如何创建这样的对话框?更好的是,有没有人有我可以使用的示例代码(这是对旧项目的扩展,我宁愿节省我的时间而不是重新发明轮子!)
谢谢
我想实现以下目标:
我可以在多个驱动器中选择多个文件夹并检索所选文件夹的文件夹路径。Qt 仅具有粗略的多文件夹选择功能,但不支持从其他驱动器等中选择文件夹。
谁能指导我如何创建这样的对话框?更好的是,有没有人有我可以使用的示例代码(这是对旧项目的扩展,我宁愿节省我的时间而不是重新发明轮子!)
谢谢
您可以使用QFileSystemModel表示 QTreeView 上的文件系统。这个例子解释了如何做到这一点。
对于复选框问题,根据此列表档案:
最简单的方法(至少我能想到)是继承 QDirModel 和 override
flags
,data
并且setData
:
flags
应该添加Qt::ItemIsUserCheckable
到返回的标志 如果角色参数是应该data
返回查询索引的应该存储索引的检查状态Qt::CheckState
Qt::CheckStateRole
setData
或者,更好的是,这应该以几乎相同的方式与 QProxyModel 一起工作(毕竟,“优先考虑对象组合而不是类继承”)。
请注意,QDirModel 类已过时。您可能不会在较新的 Qt 版本上使用它。我建议使用QFileSystemModel。
####### Retrieve a list of directories with wxPython-Phoenix - tested on python3.5
### installation instruction for wxPython-Phoenix : https://wiki.wxpython.org/How%20to%20install%20wxPython#Installing_wxPython-Phoenix_using_pip
### modified from : https://wxpython.org/Phoenix/docs/html/wx.lib.agw.multidirdialog.html
import os
import wx
import wx.lib.agw.multidirdialog as MDD
# Our normal wxApp-derived class, as usual
app = wx.App(0)
dlg = MDD.MultiDirDialog(None, title="Custom MultiDirDialog", defaultPath=os.getcwd(), # defaultPath="C:/Users/users/Desktop/",
agwStyle=MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST)
if dlg.ShowModal() != wx.ID_OK:
print("You Cancelled The Dialog!")
dlg.Destroy()
paths = dlg.GetPaths()
#Print directories' path and files
for path in enumerate(paths):
print(path[1])
directory= path[1].replace('OS (C:)','C:')
print(directory)
for file in os.listdir(directory):
print(file)
dlg.Destroy()
app.MainLoop()