0

I am trying to get a file path using QFileDialog, the compiling went good, but when I open the dialog from the application, this window pops up:

enter image description here

It looks like it is missing the letter "á" from the path.

It pops up even if I try to open Desktop from the quick access (btw. I am using windows 10 64-bit)

Qt version: 5.7.0

Compiler: Microsoft Visual C++ Compiler 14.0 (amd64)

Can someone please help me fix it?

Here is my code:

// Header:
#include <QMainWindow>
#include <QLineEdit>
#include <QFileDialog>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
    QString file = QFileDialog::getOpenFileName(this, tr("Open ui file"), "c://", tr("Qt UI Files (*.ui)"));
    ui->lineEdit->setText(file);
}
Adrián Kálazi
  • 250
  • 1
  • 2
  • 13
  • 1
    I just made a directory `QAdrián` and tested one of my Qt applications whether it is able to load the file. It did. I can make a small sample appl. – Scheff's Cat Mar 08 '17 at 20:18
  • Thanks, but I can't even open any directories which have an unicode character in their path with the dialog. I am guessing that the problem will be in some system setting or something similar. – Adrián Kálazi Mar 08 '17 at 20:35
  • Please demonstrate with a complete example, and indicate what Qt version you're using, and with what compiler. – Kuba hasn't forgotten Monica Mar 08 '17 at 20:57
  • Here you have the complete application, mostly the same as the default that Qt creator generates @KubaOber – Adrián Kálazi Mar 08 '17 at 21:40
  • 2
    @AdriánKálazi I'm a little bit suspicuous concerning the native file dialog which is used in `QFileDialog::getOpenFileName()`. (I remember that most win32 API functions are in two flavors - one with characters, one with wide characters. In your case the 1st flavor might fail due to wrong or missing encoding of `á`.) Did you try with option `QFileDialog::DontUseNativeDialog`? – Scheff's Cat Mar 08 '17 at 21:45
  • To reproduce the issue, you perhaps need to run this application in a certain location. Ensure that this application creates such a temporary location and then either relaunches itself in that location, or changes its working directory to that location. See e.g. [here](http://stackoverflow.com/a/18602568/1329652) for an example of how to relaunch a process from itself. As it stands, your problem isn't in the code, but in the environment where you launch it. – Kuba hasn't forgotten Monica Mar 08 '17 at 21:46

1 Answers1

2

I agree with your last statement that something in your systems setting might be wrong.

I used the following small sample application for test:

// standard C++ header:
#include <iostream>
#include <string>

// Qt header:
#include <QApplication>
#include <QFileDialog>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QPushButton>

using namespace std;

int main(int argc, char **argv)
{
  cout << QT_VERSION_STR << endl;
  // main application
#undef qApp // undef macro qApp out of the way
  QApplication qApp(argc, argv);
  // setup GUI
  QMainWindow qWin;
  QGroupBox qBox;
  QGridLayout qGrid;
  QPushButton qBtn(QString::fromLatin1("File Dialog"));
  qGrid.addWidget(&qBtn, 0, 0, 1, 2);
  QLabel qLbl(QString::fromLatin1("File:"));
  qGrid.addWidget(&qLbl, 1, 0);
  QLineEdit qTxt;
  qGrid.addWidget(&qTxt, 1, 1);
  qBox.setLayout(&qGrid);
  qWin.setCentralWidget(&qBox);
  qWin.show();
  // install signal handlers
  QObject::connect(&qBtn, &QPushButton::clicked,
    [&qTxt](bool) {
#if 0 // how I do it usually...
    string file = QFileDialog::getOpenFileName(&qTxt,
      QString::fromUtf8("Open File"),
      QString(),
      QString::fromUtf8("Text File (*.txt);;All Files (*)")
      ).toUtf8();
    qTxt.setText(QString::fromUtf8(file.c_str()));
#else // using QString only...
    QString file = QFileDialog::getOpenFileName(&qTxt,
      QString::fromUtf8("Open File"),
      QString(),
      QString::fromUtf8("Text File (*.txt);;All Files (*)")
      );
    qTxt.setText(file);
#endif
    });
  return qApp.exec();
}

I compiled this with VS2013, Qt Version 5.6 on Windows 10 (64 bit).

I use in my software std::string with UTF-8 encoding. (Thus, strings can be used in libraries without dependencies to Qt or any other GUI.) However, I did the same test with QString.

Snapshot of testQFileDialog.exe

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56