Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFileDialog Bug?



  • I have created a QFileDialog and use getSaveFileName to request the user for a file name. I use the option to specify allowed file name extensions (just .slx in this case). The listed existing files are properly filtered according to the specified file name extensions but when the user starts typing a file name, the list suggested file names is not filtered according to the specified file name extensions and one can in fact select a file name with an invalid extension. To me this seems a bug or have others experienced the same and solved this somehow?

    Some screen shots to illustrate the situation:
    Step1: Executing QFileDialog::getSaveFileName
    Step2: Entering text with suggested file name list
    Step3: One can select an invalid file name


  • Lifetime Qt Champion

    Hi @ModelTech,

    can you show the code you use the create the dialog?



  • I hope this parts suffices. It is primarily the last line in this code to get the QFileDialog, but I also included the part where I create the Filter of allowed File Extensions

            QStringList Extensions;
            QString Filters;
            for (int i = 0; i < Plugin->exportFileFilters().size(); ++i) {
                Filters += Plugin->exportFileFilters()[i];
                if (i < Plugin->exportFileFilters().size())
                    Filters += ";;";
                QStringList Splitted = Plugin->exportFileFilters()[i].split(QRegExp("\\(|,|\\)"), QString::SkipEmptyParts);
                for (int j = 1; j < Splitted.size(); ++j)
                    if (Splitted[j].contains("*."))
                        Extensions.append(Splitted[j].trimmed().remove("*."));
            }
    
            QString FileName = QFileDialog::getSaveFileName(this, tr("Export Model"), RecentLocation, Filters);
    

  • Lifetime Qt Champion

    This has nothing to do with Qt (esp. since this is the native Windows file dialog) - it's the same for me with a non-Qt program on Windows 8.1 ...



  • Ah, it is windows 8 indeed on which I saw this. I can also try on other platforms.

    Edit: And indeed, it's only on windows (also windows 10) but not on Linux. So, its a clear Microsoft bug... Thanks :)



  • @ModelTech
    When using Windows or Linux file dialogs at least, the filename extension type is used to filter what is offered when loading/saving, and to provide a default extension if none specified when saving. However, they do not prevent the user typing in anything else he pleases regardless.



  • @JonB : Yes, that's why I actually check what extension is eventually selected and if it is not what it should be, I add it. This is in the code right after the piece that I gave above. Thanks



  • This is no longer a Qt problem, but rather the operating system itself. Try doing the same thing with Paint, for example.

    Look:
    0_1532627765212_screen.JPG



  • It's indeed only on windows (also windows 10) but not on Linux. So, its a clear Microsoft bug...



  • @ModelTech
    It's not a bug. It's the way Windows chooses to implement the dropdown completer. They deliberately show existing files matching what you've typed regardless of extension, so that you know they exist. I quite like it.



  • I perceived it as an inconsistency and I generally do not like inconsistencies too much ;)



  • @ModelTech
    When you use http://doc.qt.io/qt-5/qfiledialog.html#getSaveFileName it takes a http://doc.qt.io/qt-5/qfiledialog.html#Option-enum parameter. Try passing it QFileDialog::DontUseNativeDialog to get the Qt dialog instead of the native Windows one. See if that behaves differently/more as you like (it'll only take a second)?


Log in to reply