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. Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory
Forum Update on Monday, May 27th 2025

Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 846 Views
  • 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.
  • N Offline
    N Offline
    nesavi
    wrote on last edited by
    #1

    I have the following code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QWidget>
    #include <QFileDialog>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents");
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    which I expected would open a dialog in c:\users\public\documents, but instead it opens the documents
    folder of the current user, namely C:\Users<username>\Documents

    If I change the input path to a subdirectory of c:/Users/public/documents, c:/Users/public/documents/foo say,
    the dialog displays the correct directory.

    Am I experiencing a bug in QFileDialog, or am I misunderstanding QFileDialog?

    For the record, I tried the same thing through an instance of QFileDialog with the same result.

    My search of this forum and through google led me to post here.

    aha_1980A jsulmJ 2 Replies Last reply
    1
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • N nesavi

        I have the following code:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        #include <QWidget>
        #include <QFileDialog>
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents");
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        which I expected would open a dialog in c:\users\public\documents, but instead it opens the documents
        folder of the current user, namely C:\Users<username>\Documents

        If I change the input path to a subdirectory of c:/Users/public/documents, c:/Users/public/documents/foo say,
        the dialog displays the correct directory.

        Am I experiencing a bug in QFileDialog, or am I misunderstanding QFileDialog?

        For the record, I tried the same thing through an instance of QFileDialog with the same result.

        My search of this forum and through google led me to post here.

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi@nesavi

        which version of Qt are you using?

        If it is not 5.12.3, have you already tried this latest version?

        Regards

        Qt has to stay free or it will die.

        N 1 Reply Last reply
        0
        • N nesavi

          I have the following code:

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          #include <QWidget>
          #include <QFileDialog>
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents");
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          

          which I expected would open a dialog in c:\users\public\documents, but instead it opens the documents
          folder of the current user, namely C:\Users<username>\Documents

          If I change the input path to a subdirectory of c:/Users/public/documents, c:/Users/public/documents/foo say,
          the dialog displays the correct directory.

          Am I experiencing a bug in QFileDialog, or am I misunderstanding QFileDialog?

          For the record, I tried the same thing through an instance of QFileDialog with the same result.

          My search of this forum and through google led me to post here.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nesavi said in Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory:

          c:/Users/public/documents

          Shouldn't it be "c:/Users/public/public documents"?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          N 1 Reply Last reply
          1
          • aha_1980A aha_1980

            Hi@nesavi

            which version of Qt are you using?

            If it is not 5.12.3, have you already tried this latest version?

            Regards

            N Offline
            N Offline
            nesavi
            wrote on last edited by
            #5

            @aha_1980

            I am running 5.12.3.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @nesavi said in Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory:

              c:/Users/public/documents

              Shouldn't it be "c:/Users/public/public documents"?

              N Offline
              N Offline
              nesavi
              wrote on last edited by
              #6

              @jsulm said in Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory:

              @nesavi said in Windows 10: QFileDialog::getOpenFileName(this, tr("Xml"), "c:/Users/public/documents"); opens wrong directory:

              c:/Users/public/documents

              Shouldn't it be "c:/Users/public/public documents"?

              Changing the path to "c:/Users/public/public documents" opens the dialog in c:/Users/public :-)

              The paths I am refering to are those written in the address bar of Windows Explorer.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                nesavi
                wrote on last edited by
                #7

                I tried opening the same directory through a C# application, with the same results. So this is most likely not an issue related to Qt.

                Thanks a lot for the replies :-)

                C# code:

                using System;
                using System.Windows.Forms;

                namespace WindowsFormsApplication1
                {
                    public partial class Form1 : Form
                    {
                        public Form1()
                        {
                            InitializeComponent();
                        }
                
                        private void button1_Click(object sender, EventArgs e)
                        {
                            var fd = new OpenFileDialog();
                            fd.InitialDirectory = @"c:\users\public\documents";
                            fd.ShowDialog();
                        }
                    }
                }
                
                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