开发者

Qt - Selecting multiple folders/directories using a dialog

开发者 https://www.devze.com 2023-03-28 04:35 出处:网络
I want to achieve something like the following: 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

I want to achieve something like the following:

Qt - Selecting multiple folders/directories using a dialog

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


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.


####### 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()
0

精彩评论

暂无评论...
验证码 换一张
取 消