<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QFileDialog::selectFile() does not keep native file separator]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">My idea was to get access to the dialog, then call selectFile() on the dialog with my predefined file names. However, this causes a <em>full path</em> 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.</p>
<p dir="auto">How can I set a filename inside a QFileDialog with the proper separator?</p>
<p dir="auto">Technical details: trying with qt 5.4.0 on Windows 7</p>
]]></description><link>https://forum.qt.io/topic/55428/qfiledialog-selectfile-does-not-keep-native-file-separator</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 08:34:16 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/55428.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 18 Jun 2015 19:48:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QFileDialog::selectFile() does not keep native file separator on Sun, 26 Jul 2015 23:06:30 GMT]]></title><description><![CDATA[<p dir="auto">Looks like there's something wrong going on, you should check the <a href="http://bugreports.qt.io" target="_blank" rel="noopener noreferrer nofollow ugc">bug report system</a> to see if it's something known.</p>
]]></description><link>https://forum.qt.io/post/283878</link><guid isPermaLink="true">https://forum.qt.io/post/283878</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 26 Jul 2015 23:06:30 GMT</pubDate></item><item><title><![CDATA[Reply to QFileDialog::selectFile() does not keep native file separator on Mon, 22 Jun 2015 12:41:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> 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:</p>
<pre><code>//// MenuExtensions.h
#include &lt;QAction&gt;
#include &lt;QFileDialog&gt;
#include &lt;QObject&gt;
#include &lt;QStandardPaths&gt;
#include &lt;QStringList&gt;
#include &lt;QWidget&gt;

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, &amp;QAction::triggered, this, &amp;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&amp; caption, const QString&amp; filter)
    {
        QFileDialog dialog(m_parent, caption, m_lastOpenDirectory, filter);
        dialog.setObjectName("OpenFileDialog");
        dialog.setFileMode(QFileDialog::ExistingFiles);
        connect(&amp;dialog, &amp;QFileDialog::directoryEntered, this, &amp;MenuExtensions::LastOpenDirectoryChanged);
        if (dialog.exec())
        {
            return dialog.selectedFiles();
        }
        return QStringList();
    }
    void LastOpenDirectoryChanged(const QString&amp; 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 &lt;QApplication&gt;
#include &lt;QDir&gt;
#include &lt;QFileDialog&gt;
#include &lt;QFileInfo&gt;
#include &lt;QMainWindow&gt;
#include &lt;QStandardPaths&gt;
#include &lt;QTemporaryFile&gt;
#include &lt;QtTest/QtTest&gt;
#include &lt;QTimer&gt;
#include "MenuExtensions.h"

void SetDirectoryAndFile(QWidget* window, const QFileInfo&amp; file)
{
    QFileDialog* dialog = window-&gt;findChild&lt;QFileDialog*&gt;("OpenFileDialog");
    dialog-&gt;setDirectory(file.absolutePath());
    dialog-&gt;directoryEntered(file.absolutePath());
    dialog-&gt;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(&amp;window);

    QTimer::singleShot(50, [&amp;window, &amp;folder, &amp;emdFile](){ SetDirectoryAndFile(&amp;window, QFileInfo(emdFile.fileName())); });
    extensions.m_fileOpen-&gt;trigger();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/279159</link><guid isPermaLink="true">https://forum.qt.io/post/279159</guid><dc:creator><![CDATA[Jakob]]></dc:creator><pubDate>Mon, 22 Jun 2015 12:41:17 GMT</pubDate></item><item><title><![CDATA[Reply to QFileDialog::selectFile() does not keep native file separator on Fri, 19 Jun 2015 22:30:07 GMT]]></title><description><![CDATA[<p dir="auto">Can you show a minimal sample code that reproduces this ?</p>
]]></description><link>https://forum.qt.io/post/278849</link><guid isPermaLink="true">https://forum.qt.io/post/278849</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 19 Jun 2015 22:30:07 GMT</pubDate></item><item><title><![CDATA[Reply to QFileDialog::selectFile() does not keep native file separator on Fri, 19 Jun 2015 06:24:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Thanks, I already found it, and I should have mentioned that - I <em>am</em> actually doing<code>selectFile(QDir::toNativeSeparators(filename))</code> - then <em>still</em> the end result is that I end up with the forward slashes in the filename instead of the backward slashes.</p>
<p dir="auto">BTW, I also realized I didn't mention that this is for the Native dialog. Could it be I hit some bug?</p>
]]></description><link>https://forum.qt.io/post/278746</link><guid isPermaLink="true">https://forum.qt.io/post/278746</guid><dc:creator><![CDATA[Jakob]]></dc:creator><pubDate>Fri, 19 Jun 2015 06:24:11 GMT</pubDate></item><item><title><![CDATA[Reply to QFileDialog::selectFile() does not keep native file separator on Thu, 18 Jun 2015 20:16:06 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You're looking for <a href="http://doc.qt.io/qt-5/qdir.html#toNativeSeparators" target="_blank" rel="noopener noreferrer nofollow ugc">QDir::toNativeSeparators</a></p>
]]></description><link>https://forum.qt.io/post/278685</link><guid isPermaLink="true">https://forum.qt.io/post/278685</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 18 Jun 2015 20:16:06 GMT</pubDate></item></channel></rss>