Show mp3 files but select folder with QFileDialog - c++

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..

Related

Selecting files with folder 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. 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
}
}

Make a Qt/C++ program show its file types as known on Windows

Using Qt 5.9 I codded a spreadsheet program and then created an installer for it by Qt Installer Framework (QtIFW2.0.1). Then I sent the program to many of my friends. They installed the app on their Windows machine and now using it, but they have all have a common problem:
when they save files of the app, those files are shown as "unknown" files on Desktop.
The problem is only with the shape and appearance of the stored files not their functionality, and they are opened by the app if double clicked.
The question is, what changes in the code is needed to have the program make its files' shape/appearance shown known?
For example, we offer the code a specific shape using an image file or like that, to be mapped on the stored files and that way they are shown known.
This has actually nothing to do with Qt or C++ itself. You just need to register your file extension in Windows shell, so it can be understood by other Windows components/shells.
Here is general information about File Types and File Associations under windows.
You need to make some Windows Registry entries which look like this:
example.reg:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\myfirm.myapp.v1\shell\open\command]
#="c:\path\to\your\app.exe \"%1\""
[HKEY_CURRENT_USER\Software\Classes\.myextension]
#="myfirm.myapp.v1"
Here you can read how it works in general
change myfirm.myapp.v1, .myextension and path to your .exe to your prefered names.
Now Windows will know what the files with extension .myextension should be opened by your app. And if you double click on this files your app will be run with path to file as an argument. You can get it in your main() function
To set icon for your extension add Registry entry in Software\\Classes\\.myextension\\DefaultIcon and set it default value to the full path to your app, so windows can get an icon for extension from your .exe app file.
You can also do it at runtime directly in your app:
QSettings s("HKEY_CURRENT_USER\\SOFTWARE\\CLASSES", QSettings::NativeFormat);
QString path = QDir::toNativeSeparators(qApp->applicationFilePath());
s.setValue(".myextension/DefaultIcon/.", path);
s.setValue(".myextension/.","myfirm.myapp.v1");
s.setValue("myfirm.myapp.v1/shell/open/command/.", QStringLiteral("\"%1\"").arg(path) + " \"%1\"");
EDIT: One more, to do it with Qt Installer look at the answers here

In the Qt how to open QFileDialog::getOpenFileNames in user home

I'm using Qt QFileDialog::getSaveFileName and QFileDialog::getOpenFileNames for the user to select where to save files and what files to open in my app.
The third parameter of this function is the path where you want the window to open by default. In linux, How can I get the dialog to open in the user home, and in windows how can I get the dialog to open in the user user folder in win 7 or in 'My Documents' in win xp?
Currently I'm using the dialog like this: QFileDialog::getOpenFileNames(this, "Select a file to open...", HOME); where HOME is a preprocessor macro that in UNIX is ~ and in windows is C:\
The Unix one does not work and opens the dialog in the same folder where the binary is.
Use QDir::homePath.
QFileDialog::getOpenFileNames(this, "Select a file to open...", QDir::homePath())
Also if you want to apply a filter on existing files, you can try this:
QString filter = "File Description (*.extention)";
// For example: "Mpeg Layer 3 music files (*.mp3)"
QFileDialog::getOpenFileName(this, "Select a file...", QDir::homePath(), filter);
And then once user selected a file, absolute address of that file is returned by QFileDialog::getOpenFileName function.

Opening files from Finder with a Qt-based application?

Apparently, for Cocoa applications, you're supposed to implement [[NSApp delegate] application:openFile:] or something like that to allow your application to open files double clicked in Finder.
How do you achieve this functionality using Qt, as the name of the file to be opened is not passed on the command line?
QFileOpenEvent (Qt4/Qt5) should do the trick.
Also see https://doc.qt.io/archives/qq/qq18-macfeatures.html

How do you set icons of .exe files?

Preferably using C++. Or a tool I can use from the command line. So far I've figured out how to extract icons from .exe files, but I can't set icons... Any suggestions?
If you want an icon to show in Explorer in a directory list, you'll need to add an icon to your app's .rc file. It must be the first icon in the resource file.
Do you want an icon to appear in the task bar and have it associated with your app's window? In this case you must set the hIcon member in your registered WNDCLASS structure.
Here's a code sample that might help:
Change Icon of EXE file through code extracting it from other EXE file