2

I have a problem with coding. I'm using drag and drom inside my application but some text files I can not open, after some searching I find that the path is with wrong coding. The real file is 'Some_file - 01.txt' but when I try print this path (after drop) to the stdout I will get 'Some_file – 01.txt'. What I miss:

void MainWindow::dropEvent(QDropEvent *event) {
  QList<QUrl> urls = event->mimeData()->urls();
  ...
  cout << paths[1].toLocalFile() << endl; /* Some_file – 01.txt */
  cout << paths[1].toEncoded() << endl; /* Some_file%20%E2%80%93%2001.txt */
}

I also try QString::fromLatin1 or fromUtf8 but without success. I'm using QT 4.7.0 and Windows 7.

Edit:

This is my main setup:

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

And unfortunately even this is not working for me:

QString fileName = QFileDialog::getOpenFileName(this, tr("Load EEPROM from HEX file"), "", tr("HEX file (*.hex)"));
ifstream hexFile(fileName.toStdString().c_str());

I'm not able to open files where the char '-' is part of file name.

EDIT2:

If I change the file name manualy from 'file.txt' to 'file-.txt' everything is working well. But when I (the same file) copy and paste this file to the same folder, windows will generate new name with postfix: 'file - copy.txt' and this file I can NOT open. So the Windows is using different character for '-' vz. '–'.

What I can do ?

Solution:

void openFile(string fileName) {
  ifstream fileio(fileName.c_str());
}

QString qtFileName = QFileDialog::getOpenFileName(...)
openFile(qtFileName.toLocal8Bit().constData());
Lodhart
  • 205
  • 1
  • 6
  • 14
  • Your codec settings are likely invalid since you need to be using the local 8 bit encoding. The `toStdString` function will return a UTF-8 encoded string which may well *not* be what `ifstream` expects. – Kuba hasn't forgotten Monica Jul 04 '14 at 11:59
  • Essentially, you never need to set the codec. You need to pass a `toLocal8Bit` encoding to `ifstream`. Finally, you can use `QFile` and `QTextStream`, then you don't need to worry about it. – Kuba hasn't forgotten Monica Jul 04 '14 at 12:01
  • Note that Windows usually does *not* expect UTF-8 encoded strings, and you simply can't use `ifstream` for certain files as there's no way to represent their names in the 8-bit encoding that Windows expects. In any case, use **`toLocal8Bit`** when passing things to the operating system via an 8 bit encoding, **not `toStdString`**! – Kuba hasn't forgotten Monica Jul 04 '14 at 12:04
  • Many thanks. Now it is working for me. I'm using my C++ libraries with Qt as a tool for GUI and these lib are using ifstream, string, ... So problem was as you mention in your first comment. I just did not see it. The solution is simple. Thx. – Lodhart Jul 07 '14 at 08:07

1 Answers1

3

std::cout is encoded with some local encoding. What you need is to convert the QString returned by the toLocalFile() into a local 8 bit encoding.

For example:

QUrl url = ...;
QString filePath = url.toLocalFile();
QByteArray filePath8 = filePath.toLocal8Bit();
std::cout << filePath8.constData();

But really, the whole exercise is not necessary, since to access the files you should be using QFile, which takes a QString directly, and console output can be done using QTextStream. To wit:

#include <cstdio>
#include <QTextStream>
#include <QFile>
QTextStream out(stdout);

void test() {
  out << filePath;

  QFile file(filePath);
  if (file.open(QIODevice::ReadOnly)) {
    ...
  }
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Yes, but I have a problem even with the 'QFileDialog::getOpenFileName(...)' which is returning a QString. So I think that I have to use your first example. – Lodhart Jul 04 '14 at 07:49
  • @Lodhart You're writing in Qt, you don't really need to convert from `QString` to anything else just to deal with file paths etc. Your problem wasn't with wrong contents of the file path, but with invalid attempts at converting it. Skip the conversion, and you won't have a problem. So, what **exactly** is your problem with `getOpenFileName`? – Kuba hasn't forgotten Monica Jul 04 '14 at 11:58
  • I'm not able to open file which contains character '–'. I'm using standart QString fileName = QFileDialog::getOpenFileName(..) and ifstream for open the file. – Lodhart Jul 07 '14 at 07:57