QFileDialog::selectFile() does not keep native file separator



  • While trying to test the behaviour behind a QFileDialog I'm trying to explicitly set some file names, in order to test for the correct backend behaviour in my code.

    My idea was to get access to the dialog, then call selectFile() on the dialog with my predefined file names. However, this causes a full path to be entered in the dialog, and it does not use the Windows separator. As a result I can't let my test simulate the 'Enter' to accept the filename, because Windows will complain that the file name is invalid.

    How can I set a filename inside a QFileDialog with the proper separator?

    Technical details: trying with qt 5.4.0 on Windows 7


  • Lifetime Qt Champion

    Hi,

    You're looking for QDir::toNativeSeparators



  • @SGaist Thanks, I already found it, and I should have mentioned that - I am actually doingselectFile(QDir::toNativeSeparators(filename)) - then still the end result is that I end up with the forward slashes in the filename instead of the backward slashes.

    BTW, I also realized I didn't mention that this is for the Native dialog. Could it be I hit some bug?


  • Lifetime Qt Champion

    Can you show a minimal sample code that reproduces this ?



  • @SGaist Yes I can, I tried to reduce as much as possible of the encapsulating boiler code to create a self-contained example. I still kept some stuff in that is maybe not needed, but because I'm not sure where the problem is, I kept it in nevertheless, such that I don't hide any magic:

    //// MenuExtensions.h
    #include <QAction>
    #include <QFileDialog>
    #include <QObject>
    #include <QStandardPaths>
    #include <QStringList>
    #include <QWidget>
    
    class MenuExtensions : public QObject
    {
        Q_OBJECT
    public:
        MenuExtensions(QWidget* parent)
            : m_fileOpen(new QAction(parent))
            , m_parent(parent)
            , m_lastOpenDirectory(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation))
        {
            connect(m_fileOpen, &QAction::triggered, this, &MenuExtensions::HandleOpenFile);
        }
    private slots:
        void HandleOpenFile()
        {
            QStringList requestedFiles = OpenFileDialog(QString("Open experiment"), QString("Experiment files (*.emd)"));
            // call the loading interface in the business logic 
        }
    private:
        QStringList OpenFileDialog(const QString& caption, const QString& filter)
        {
            QFileDialog dialog(m_parent, caption, m_lastOpenDirectory, filter);
            dialog.setObjectName("OpenFileDialog");
            dialog.setFileMode(QFileDialog::ExistingFiles);
            connect(&dialog, &QFileDialog::directoryEntered, this, &MenuExtensions::LastOpenDirectoryChanged);
            if (dialog.exec())
            {
                return dialog.selectedFiles();
            }
            return QStringList();
        }
        void LastOpenDirectoryChanged(const QString& dir)
        {
            m_lastOpenDirectory = dir;
        }
    public: // public just for the purpose of this minimal example
        QAction* m_fileOpen;
        QWidget* m_parent;
        QString m_lastOpenDirectory;
    };
    
    /// TestMinimum.cpp
    #include <QApplication>
    #include <QDir>
    #include <QFileDialog>
    #include <QFileInfo>
    #include <QMainWindow>
    #include <QStandardPaths>
    #include <QTemporaryFile>
    #include <QtTest/QtTest>
    #include <QTimer>
    #include "MenuExtensions.h"
    
    void SetDirectoryAndFile(QWidget* window, const QFileInfo& file)
    {
        QFileDialog* dialog = window->findChild<QFileDialog*>("OpenFileDialog");
        dialog->setDirectory(file.absolutePath());
        dialog->directoryEntered(file.absolutePath());
        dialog->selectFile(QDir::toNativeSeparators(file.fileName()));
    
        // Next follows the problematic 'enter', which will produce 'The filename is not valid',
        // because the file dialog changed the native separator back into '/'
        QTest::keyClick(dialog, Qt::Key_Enter);
    }
    
    int main(int argc, char** argv)
    {
        QApplication app(argc, argv);
    
        QDir folder(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
        QDir::setCurrent(folder.absolutePath());
        QTemporaryFile emdFile("ExperimentXXXXXX.emd");
        emdFile.open();
    
        QMainWindow window;
        MenuExtensions extensions(&window);
    
        QTimer::singleShot(50, [&window, &folder, &emdFile](){ SetDirectoryAndFile(&window, QFileInfo(emdFile.fileName())); });
        extensions.m_fileOpen->trigger();
    }
    

  • Lifetime Qt Champion

    Looks like there's something wrong going on, you should check the bug report system to see if it's something known.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.