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.
Related
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
}
}
I need to create a push button that will open a .doc or a .pdf file. It only works when I use a QFileDialog like this:
QString docName = QFileDialog::getOpenFileName(this, tr("Arquivos"), "C:/Users/lucas/Desktop/HEMODIALISE/Hemodialise/Resources/Documentos", "All files (*.*)");
QDesktopServices::openUrl(QUrl("file:///"+docName, QUrl::TolerantMode));
But I want to open instantly using just the file inside the resources of the program.
How can I accomplish that?
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..
How can I check is specific directory opened by a user (Windows) in c++ (WinAPI)?
For example, I have program in \user\My Documents\checker.exe.
checker.exe is running in background and checking if user open My Documents directory. If this is true, program save system time to a file.txt.
Thank you for help!
You must first get the PIDL of the user's "My Documents" folder using either:
SHGetDesktopFolder() and IShellFolder::ParseDisplayName(), or SHParseDisplayName(). Specify L"::{450d8fba-ad25-11d0-98a8-0800361b1103}" as the display name to parse (see My Documents and My Pictures Folders).
SHGetFolderPath(CSIDL_MYDOCUMENTS)
SHGetKnownFolderPath(FOLDERID_Documents) (Vista and later only)
Then, you can enumerate all Explorer windows, comparing the PIDLs from each window. If any window's PIDL matches/begins with the retrieved PIDL, that window has the "My Documents" folder open, or a (grand)child folder.
I want to use a native file dialog to let the user choosing a .zip file
So I call following Qt static method with zip extension as expected file type
QFileDialog::getOpenFileName(0, "Please select a zip file", "/home", "*.zip");
The behaviour is OK, the dialog is well expecting a ZIP file but the extension is not explicitly displayed anywhere on the dialog box.
That can be disturbing for the user.
I try to find some others file dialogs in other apps under Mac OSX (Mavericks), I found one with an additional bottom panel showing the expected file type.
Anyone knows how can I also get the same kind of file dialog showing the expected file type?