QFileDialog showing hidden files although system setting is off - c++

I am using the following code to show an open dialog in Qt:
QString path = QFileDialog::getOpenFileName(this, tr("Open Config File"), QDir::rootPath(), "Text Files (*.txt *.csv *.*);;");
What I realised is that this dialog also shows hidden files although the system setting for showing hidden files is turned off. It's the same if I instantiate the QFileDialog manually and show it. I also couldn't find out how to turn this off via a filter.
Does anyone know if there is a way to achieve the desired behaviour?

Looks like there is no simple(by setting some flag) solution out there. So I recommend to use the filtering which is described in other SO answer.
But in your case you might use the following condition:
if(fileModel != nullptr)
{
QFileInfo info = fileModel->fileInfo(index0);
return info.isHidden();
}
return false;

Related

QFileDialog::getOpenFileName poor performance and user experience

I want to let my users select an existing file from the filesystem. Looks like Qt's solution is QFileDialog. I looked up the docs and came up with something like this:
void MainWindow::openFileDialog()
{
const QString& inifilePath =
QFileDialog::getOpenFileName(this,
tr("Select Ini file"),
QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
tr("Ini files (*.ini)"));
qDebug() << inifilePath;
}
MainWindow is my main window class. The problem is that file dialog window opens unacceptably slowly. After that it is still glitchy. If you right-click on something on the side bar, it again takes forever to show the rightclick options and you get a runtime warning-like output:
***********Create CommApi2Trayhelper************
***********Destruct CommApi2TrayHelper************
**********CommApi2TrayHelper::UnInitCheck*************
When the user selects some file and clicks OK program gives another runtime warning:
shell\comdlg32\fileopensave.cpp(14403)\comdlg32.dll!00007FFD757795AE: (caller: 00007FFD757A8291) ReturnHr(1) tid(1d9c) 80004005 Unspecified error CallContext:[\PickerModalLoop]
All these lead me to believe that I am doing something wrong, even though I am doing exactly what the examples in the docs are doing. What am I doing wrong?

QFileDialog: add suffix after selecting file

I need to add suffix to selected filename in QFileDialog with QFileDialog::AcceptSave accept mode. For example, after selecting "1.txt" file in QFileDialog edit should be select "1_suffix.txt". It should be added before file accepting, because I need the user to have the ability to change the filename before applying file.
code:
m_dialog.setAcceptMode(QFileDialog::AcceptSave);
m_dialog.setWindowModality(Qt::WindowModal);
m_dialog.setFileMode(QFileDialog::AnyFile);
m_dialog.setDefaultSuffix("_suffix");
if(m_dialog.exec() == QFileDialog::Accept)
{
setPath(m_dialog.selectedFiles()[0]);
}
Usually, a QFileDialog is displaying the platform file dialog. To get the behavior you want, you'd need to use platform-specific mechanisms; Qt doesn't implement such functionality.
If you're using the non-native file dialog, you could inspect its structure to find the widget(s) you're after, filter relevant events on them, and inject the behavior you need.
Try extending QFileDialog and subscribe to QFileDialog signals
void fileSelected(QString file)
void currentChanged(QString path)
It can be a start.

Trouble with getting national characters in QFileDialog working

I'm trying to take start using Qt and decided to poke around this text editor first.
Everything goes fine except some strange behaviour of QFileDialog - I can not manage it to deal with national charset.
Here is what I see when running compiled binary and trying to open file:
I've tried to search the docs and qt wiki in order to get some clues, but there is nothing about i18n that could be readable for newbie like me.
So here is the problem code:
void TextEdit::fileOpen()
{
QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
if (!fn.isEmpty())
load(fn);
}
I guess I should attach ::fromUtf8() here somehow, but have no understanding of how this should be done.
UPD
Tried to change default locale just as advised:
QLocale curLocale(QLocale("ru_RU"));
QLocale::setDefault(curLocale);
Also tried this variant:
QLocale::setDefault(QLocale(QLocale::Russian, QLocale::RussianFederation));
This did not help, problem persists.

QFileDialog: using getOpenFileName allow for non-existent files

I want to program a browse button with qt that opens a standard find file dialog. If the user enters a new file name in the dialog I want to create the file. If the file exists I want to open it.
I have a function that given a string will make that decision. However, QFileDialog::getOpenFileName shows the user a error if the file doesn't exist, and QFileDialog::getSaveFileName asks the user for a confirmation to overwrite the file if it does exist (which I wouldn't do anyways, so it should not be showed).
Is there a qt standard implemented that could meet my need without having to create a custom class iheriting from QFileDialog or resorting to another similarly hairy situation?
Here is my current working code, with undesired behavior...
void Login::browseFile() {
QString file = ui->txtFile->text();
if (file.isEmpty()) { file = QDir::homePath(); }
file = QFileDialog::getOpenFileName(this,
tr("Select Monage Database"), file,
tr("Database Files (*.db)"));
if (!file.isEmpty()) { OpenDb(file); }
}
Google failed me, but a few more minutes scrutinizing the docs, and I found this:
QFileDialog::DontConfirmOverwrite 0x00000004 Don't ask for confirmation if an existing file is selected. By default confirmation is requested.
I was able to use this for getSaveFileName to achieve the functionality I desired. I had to specify the option selectedFilter, but just passed the default 0.
Modified code:
void Login::browseFile() {
QString file = ui->txtFile->text();
if (file.isEmpty()) { file = QDir::homePath(); }
file = QFileDialog::getSaveFileName(this,
tr("Select Monage Database"), file,
tr("Database Files (*.db)"), 0,
QFileDialog::DontConfirmOverwrite);
if (!file.isEmpty()) { OpenDb(file); }
}

QFiledialog returns the incorrect directory

A snippet of what i'm using looks like this
QDir lastDir;
QFileDialog dial(this);
dial.getOpenFileName(this,
tr("Open File"),
QString("/home"),
tr("Raw Images (*.nef *.NEF *.dng *.DNG)"));
lastDir = dial.directory();
qDebug() << lastDir;
The output, is completely wrong, no matter which directory I end up in. However, the incorrect directory is always the same.
AFAICT i'm doing nothing wrong here. What is going on here? Cheers
getOpenFileName() is a static function which immediately opens a "file picker" dialog and returns, once the user is finished with the dialog, "an existing file selected by the user". You use it like this (note the use of :: and the class name QFileDialog instead of the object name):
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"),
QString("/home"),
tr("Raw Images (*.nef *.NEF *.dng *.DNG)"));
directory() is non-static and returns "the directory currently being displayed in the dialog". This function is meant to be called while the dialog is still open, it's intended for use cases which are not covered by the static calls.
What is happening here is you have instantiated an object, called a static function on it (which won't affect its state), and then called directory() which will just reflect the original state of the object, which is probably the working directory. Instead, you need to store the return value of the getOpenFileName() call in a variable, as shown above.
If you want to ask the user to just choose a directory, you could consider using getExistingDirectory() instead. Alternatively, if you want to extract the directory from the filename, the QDir class has some functions useful for this.