Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog::selectFile() does not keep native file separator
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jakob
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You're looking for QDir::toNativeSeparators

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You're looking for QDir::toNativeSeparators

        J Offline
        J Offline
        Jakob
        wrote on last edited by Jakob
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Can you show a minimal sample code that reproduces this ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Can you show a minimal sample code that reproduces this ?

            J Offline
            J Offline
            Jakob
            wrote on last edited by
            #5

            @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();
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved