0

I using explorer for show files and "select" in Windows 7 and Windows 8, like this:

explorer /select,c:\foo\bar\baz.txt

In Qt I tried use:

const QString filepath = a.applicationDirPath() + "\\.txt";
QStringList params = QStringList() << "/select," << filepath;
QProcess::startDetached("explorer", params);

Work fine in Windows 10, but not work in Windows 7 and 8 if file path contains unicode characters ("emojis").

In my folder I put two files in release folder:

app.exe
.txt

My source:

#include <QCoreApplication>
#include <QProcess>
#include <QDebug>
#include <QDir>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    const QString name = ".txt";

    const QString filepath = QDir::toNativeSeparators(a.applicationDirPath() + "\\" + name);

    QStringList params = QStringList() << "/select," << filepath;

    qDebug().noquote() << "explorer " + params.join("");

    QProcess::startDetached("explorer", params);

    return a.exec();
}

I tried use chcp 65001:

QProccess::execute("chcp 65001");
QProccess::startDetached("explorer", params);

After I tried:

const QString name = QString::fromWCharArray(L".txt");

And:

const QString name = QString::fromStdWString(L".txt");

But not work.

How "encode" filepath for use with /select, argument? Or is it not possible to use unicode in Windows 7 and Windows 8?

Protomen
  • 9,471
  • 9
  • 57
  • 124
  • `QString` contains Unicode characters, but you are using narrow string literals in your code that are subject to the compiler's particular character encoding. `QString` expects narrow strings to be UTF-8. Try using wide literals instead and convert them to `QString`, eg: `const QString filepath = a.applicationDirPath() + QString::fromWCharArray(L"/.txt");` or `const QString filepath = a.applicationDirPath() + QString::fromStdWString(L"/.txt");` – Remy Lebeau Jul 27 '17 at 19:59
  • @RemyLebeau thanks, but not work :( – Protomen Jul 27 '17 at 20:11
  • It doesn't work on the Start > Run menu, or in the CMD prompt, either. So this is obviously an Explorer limitation in Windows 7 that was fixed in Windows 10. Nothing you can do about it at the command-line. Try using [`SHOpenFolderAndSelectItems()`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762232.aspx) instead, passing it the PIDL of the desired file, which you can get from [`ILCreateFromPath()`](https://msdn.microsoft.com/en-us/library/windows/desktop/dd378420.aspx) or [`SHParseDisplayName()`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762236.aspx). – Remy Lebeau Jul 27 '17 at 20:17
  • @RemyLebeau nice, I will try! Thanks! – Protomen Jul 27 '17 at 20:20
  • May not be related, but for any string literal that contains Unicode in Qt that you want to use with QString, you should always wrap it in [QStringLiteral](http://doc.qt.io/qt-5/qstring.html#QStringLiteral) to avoid encoding issues. – MrEricSir Jul 27 '17 at 22:03

0 Answers0