Selecting files with folder using QFileDialog - c++

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.

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
Constant
Value
Description
QFileDialog::Directory
2
The 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
}
}

Related

Selecting files recursively using QFileDialog

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. The files are selected when a root folder is selected using a code snippet as shown below.
void MainWindow::on_browseButton_clicked()
{
QDir directory = QFileDialog::getExistingDirectory(this);
zipFiles = directory.entryList(QStringList() << "*.zip", QDir::Files);
foreach(QString filename3, zipFiles) {
qDebug() << filename3;
}
}
This works fine for normal files (.zip files with no other inner .zip file inside them). But I have a use case in which I need to select inner .zip files inside a zip file. How can I achieve this using the above code snippet.

Show mp3 files but select folder with QFileDialog

Currently, I can select a folder using QFileDialog's getExistingDirectory function. Even thought I managed to let the dialog show the files as well, I would like to filter them, i.e. display only folders and *.mp3 files.
QString folder = QFileDialog::getExistingDirectory(this, "test", "", QFileDialog::ReadOnly);
Is this possible without creating a custom dialog?
QFileDialog d(this);
d.setFileMode(QFileDialog::Directory);
d.setNameFilter("*.mp3");
if (d.exec())
qDebug () << d.selectedFiles();
I am on mobile rt now. Giving a kind of hints only....
On Windows and macOS, this static function will use the native file dialog and not a QFileDialog. However, the native Windows file dialog does not support displaying files in the directory chooser. You need to pass DontUseNativeDialog to display files using a QFileDialog. ( from qfiledialog documentation)
So set the 'QFileDialog::DontUseNativeDialog' option using 'setflags'
Then using file dialog's 'setfilter' set the filter for your MP3 files..

c++ MFC accessing multiple files

I'm working with visual studio 2012.
In status quo, in MFC form there is a input box and it takes entire directory path of mp3 file which will be used in MFC. However, the files should be clicked and selected to be executed and this is very laborious job.
Currently, the file path is saved in this->tb_filePath->Text form in MFC, and after the private: System::Void Run_Click(System::Object^ sender, System::EventArgs^ e) is clicked, a function gets the file path saved above and executes.
Since all the mp3 files are saved in specific directory, I tried to make this process automatically. Also in directory,
xxxxx.mp3
aaxxx.txt
xxxx1.mp3
aaxx1.txt
there are mp3 formats and txt formats, I have to distinguish those files also. txt is not allowed as an input. But I'm not familiar with window-MFC form right now, so I'm not sure how can I start with this.
First, how can I automatically access to the directory and read the files that will be used in the .exe program. And how can I know that I have found all the files?
Second is it possible to select next files in folder when I press the certain button of the MFC?
Overall, I am not sure how can I search through all the .mp3 files in certain directory without selecting it one by one.
This was solved. It was just a problem dealing with directory and file relations. sorry for the poor questioning.

ShellExecute to open a special folder (ex: "Libraries\Documents") without knowing the special folder name

I'm making an application to backup the opened folders.
The problem is that if in that folders are some special folders (ex: "Desktop", "Computer", "Libraries\Documents", ...) they will not open.
I know that there are constants for those objects, but I don't know which folders will be opened so making a dictionary with all SpecialNames => SpecialConstant is not a good solution for me.
So the question is:
Is there any WinApi function to retrieve the full path from a short name of a special folder?
P.S. Tried both ShellExecute("open", "Path") and ShellExecute("open", "explorer.exe", "Path")
If you paste the names in the explorer they work, but opening them from C++ doesn't work
Thanks
Libraries are stored in the users %appdata%\Roaming\Microsoft\Windows\Libraries directory as XML files with the names <libraryname>.library-ms (e.g. Pictures.library-ms.) Opening one of these files with ShellExecute opens an Explorer window and shows the library.

ifstream can't locate file?

I'm trying to read a file called "sample.txt" but whenever i try to do an ifstream, it can never locate the file. So my question is where do you put the file so that it can be located?
ifstream iFile("sample.txt");
Here is the solution.
1) Open your project in Xcode;
2) Locate the Products folder in the Groups & Files tree on the left side of your Project window;
3) When you expand that folder, you should see the icon for the Unix Executable File (usually a black rectangle);
4) Ctrl-click on that icon and select Show in Finder_ (If you accidentally choose Open With Finder, the Finder will start your program in a Terminal window);
5) Show in Finder should bring up a Finder window showing the contents of the folder which includes your executable. You should see your output files in that window.
Source: https://discussions.apple.com/thread/2145737?start=0&tstart=0
If you don't specify an absolute path the file name is interpreted to be relative to the working directory of the program.