QFileDialog for directories that have certain content

时光毁灭记忆、已成空白 提交于 2019-12-24 01:38:10

问题


I would like to build a dialog similar to QFileDialog::getExistingDirectory() for which the OK-button only is enabled when the selected directory contains certain files.

I know I cannot achieve this with QFileDialog, instead I would have to come up with my own QDialog that has a QTreeView coupled to a QFileSystemModel.

  1. How can I limit the QTreeView to directories?
  2. How can I get the currently selected directory so I can check whether it contains some filenames?

回答1:


  1. Use setFilter on the QFileSystemModel with either the QDir::AllDirs or QDir::Dirs option, probably the former.
  2. connect the activated(QModelIndex) signal from the treeview to a custom slot of yours. In this slot pass the QModelIndex to the model's fileInfo/filePath method, to retrieve the info/path for the selected directory, then perform your check

Here is an example:

void slotDirectorySelected( const QModelIndex & index )
{
    QFileInfo info = fileSystemModel->fileInfo( index );
    QDir selectedDir = info.dir();
    foreach( const QString entry, selectedDir.entryList() ) {
        // do something with the entry
    }
}


来源:https://stackoverflow.com/questions/3288924/qfiledialog-for-directories-that-have-certain-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!