使用qfiledialog选择文件夹的文件

发布于 2025-01-24 22:16:44 字数 645 浏览 0 评论 0原文

我在应用程序中使用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.

enter image description here

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

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

发布评论

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

评论(1

柠北森屋 2025-01-31 22:16:44

它仅显示未选择文件的文件夹名称。

那就是它应该返回的。您要求它显示一个对话框以选择一个文件夹,以便您可以选择的一切。 selectedfiles()根据文档将返回所选文件夹的路径:

https://doc.qt.io/qt-5/qfiledialog.html#filemode-enum

常数value描述
qfiledialog :: Directory2目录的名称。显示文件和目录。但是,本机Windows文件对话框不支持在目录选择器中显示文件。

https://doc.qt.io/qt.io/qt-5/qfiledialog.html.html# SelectedFiles

返回对话框中所选文件的绝对路径的字符串列表。如果未选择文件,则或该模式不是easterfileseasunfileselectedfiles()包含视图中的当前路径< /strong>。


对话框已关闭,exec()已返回,然后您需要自己迭代该文件夹才能发现.zip.7z中的文件。

处理对话框的一种更简单的方法是使用而不是。您可以构造 qdir qdir ,然后使用 qdir :: entrylist :: entryList() 在文件夹中搜索zip文件的方法。

请参阅如何从选定目录中读取所有文件并一个人使用它们?

例如:

void MainWindow::on_browseButton_clicked()
{
    QDir directory = QFileDialog::getExistingDirectory(this);
    QStringList zipFiles = directory.entryList(QStringList() << "*.zip" << "*.7z", QDir::Files);
    foreach(QString filename, zipFiles) {
        // do whatever you need to do
    }
}

it displays just the folder name not with no files selected.

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

ConstantValueDescription
QFileDialog::Directory2The name of a directory. Both files and directories are displayed. However, the native Windows file dialog does not support displaying files in the directory chooser.

https://doc.qt.io/qt-5/qfiledialog.html#selectedFiles

Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles or ExistingFile, selectedFiles() contains the current path in the viewport.

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 a QDir from the selected folder, and then use the QDir::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:

void MainWindow::on_browseButton_clicked()
{
    QDir directory = QFileDialog::getExistingDirectory(this);
    QStringList zipFiles = directory.entryList(QStringList() << "*.zip" << "*.7z", QDir::Files);
    foreach(QString filename, zipFiles) {
        // do whatever you need to do
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文