【问题标题】:Qt - Selecting multiple folders/directories using a dialogQt - 使用对话框选择多个文件夹/目录
【发布时间】:2011-08-12 11:32:49
【问题描述】:

我想实现以下目标:

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

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

谢谢

【问题讨论】:

  • 我认为你必须自己编写代码。
  • 没错,但我想知道如何处理它。子类什么,实现什么?等

标签: qt folderbrowserdialog


【解决方案1】:

您可以使用QFileSystemModel 表示QTreeView 上的文件系统。 This example 解释了如何做到这一点。

对于复选框问题,根据this list archives

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

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

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

请注意,QDirModel class 已过时。您可能不会在较新的 Qt 版本上使用它。我推荐使用QFileSystemModel

【讨论】:

    【解决方案2】:
    ####### 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()
    

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2011-03-07
      • 2011-04-29
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 2014-01-14
      相关资源
      最近更新 更多