使用qfiledialog选择文件夹的文件
我在应用程序中使用C ++和QT(在Windows 10上)有一个用例。该应用程序使用7ZIP.EXE作为子过程,以在选定的文件夹中取消压缩文件。我需要使用qfiledialog选择一个文件夹,并使用Extension .zip和.7z获取所有文件,以自动选择,然后使用QProcess取消压缩它们,然后在输出中显示它们。 我想出了这个代码段。用于选择使用选定文件夹的文件。
void MainWindow::on_browseButton_clicked()
{
QFileDialog d(this);
d.setFileMode(QFileDialog::Directory);
d.setNameFilter("*.zip");
if (d.exec())
qDebug () << d.selectedFiles();
}
但是此代码不运行,并且仅显示未选择文件的文件夹名称。谁能建议我在哪里做错了。
I have a use case in an application using C++ and Qt (on windows 10). The application uses 7zip.exe as a subprocess to uncompress the files in a selected folder. I need to use QFileDialog to select a folder, and get all the files with extension .zip and .7z, to be selected automatically and then uncompress them using QProcess and display them in the output.
I came up with this code snippet. For selecting the files with selected folders.
void MainWindow::on_browseButton_clicked()
{
QFileDialog d(this);
d.setFileMode(QFileDialog::Directory);
d.setNameFilter("*.zip");
if (d.exec())
qDebug () << d.selectedFiles();
}
but this code does not run, and it displays just the folder name not with no files selected. Could anyone suggest where I am doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那就是它应该返回的。您要求它显示一个对话框以选择一个文件夹,以便您可以选择的一切。
selectedfiles()
根据文档将返回所选文件夹的路径:https://doc.qt.io/qt-5/qfiledialog.html#filemode-enum
https://doc.qt.io/qt.io/qt-5/qfiledialog.html.html# SelectedFiles
在对话框已关闭,
exec()
已返回,然后您需要自己迭代该文件夹才能发现.zip
和.7z
中的文件。处理对话框的一种更简单的方法是使用 ) 而不是。您可以构造
qdir
qdir ,然后使用
在文件夹中搜索zip文件的方法。qdir :: entrylist :: entryList()
请参阅如何从选定目录中读取所有文件并一个人使用它们?。
例如:
That is what it is supposed to return. You asked it to display a dialog to select a folder, so that is all you can select.
selectedFiles()
will return the path to the selected folder, per the documentation:https://doc.qt.io/qt-5/qfiledialog.html#FileMode-enum
https://doc.qt.io/qt-5/qfiledialog.html#selectedFiles
After the dialog has closed and
exec()
has returned, you will then need to iterate that folder yourself to discover.zip
and.7z
files in it.An easier way to handle the dialog is to use
QFileDialog::getExistingDirectory()
instead. You can construct aQDir
from the selected folder, and then use theQDir::entryList()
method to search for zip files in the folder.See How to read all files from a selected directory and use them one by one?.
For example: