Qt - 使用对话框选择多个文件夹/目录

发布于 2024-11-29 15:30:05 字数 255 浏览 5 评论 0原文

我想要实现类似以下的目标:

在此处输入图像描述

我可以在多个驱动器上选择多个文件夹并检索文件夹所选择的路径。 Qt 仅具有粗略的多文件夹选择功能,但它不支持从其他驱动器等选择文件夹。

任何人都可以指导我如何创建这样的对话框吗?更好的是,是否有人有我可以使用的示例代码(这是对旧项目的扩展,我宁愿节省时间而不是重新发明轮子!)

谢谢

I want to achieve something like the following:

enter image description here

Where I can select multiple folders across multiple drives and retrieve the folder paths of those selected. Qt only has a crude multi-folder selection feature, but it does not support selected folders from other drives etc.

Can anyone guide me on how to create such a dialog? Better yet, does any one have any sample code I could use (this is an extension to an old project, and I'd much rather save my time and not re-invent the wheel!)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

櫻之舞 2024-12-06 15:30:05

您可以使用 QFileSystemModel 来表示 QTreeView此示例说明了如何执行此操作。

对于复选框问题,根据此列表档案

最简单的方法(至少我能想到)是子类化
QDirModel 并覆盖 flagsdatasetData

flags 应将 Qt::ItemIsUserCheckable 添加到返回的标志中
如果角色参数为 Qt::CheckStateRoledata 应返回查询索引的 Qt::CheckState
setData 应存储索引的检查状态

或者,更好的是,这应该与 QProxyModel 一起工作
同样的方式(毕竟,“优先考虑对象组合而不是类
继承”)。

请注意,QDirModel 类 已过时。您不能在较新的 Qt 版本。我建议使用 QFileSystemModel

You can use QFileSystemModel for represent filesystem on QTreeView. This example explains how to do that.

For checkbox issue, according to this list archives:

The simplest way to do this (I can think of, at least) is to subclass
QDirModel and override flags, data and setData:

flags should add Qt::ItemIsUserCheckable to the returned flags
data should return the Qt::CheckState of the queried index if the role parameter is Qt::CheckStateRole
setData should store the check state of the index

Or, even better, this should work with a QProxyModel pretty much the
same way (after all, "favor object composition over class
inheritance").

Note that QDirModel class is obsolete. You may not use that on newer Qt versions. I recommend to use QFileSystemModel.

我最亲爱的 2024-12-06 15:30:05
####### 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()
####### 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文