在程序中直接使用Qt的QDesktopServices::openUrl()函数调用桌面应用打开本地的文件,文件名称没有中文没有问题。但文件名中含有中文时,QUrl解析出错,打开文件失败。
解决方法:
(1)文件使用绝对路径 fileInfo.absoluteFilePath()
(2)使用QUrl::fromLocalFile(),传入绝对路径
QDir dir("../");
QStringList nameFilters;
nameFilters << "*.pdf";
QFileInfoList fileList = dir.entryInfoList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
if (fileList.isEmpty())
return;
QString path = fileList.at(0).absoluteFilePath(); //filePath();
if (QFile::exists(path))
{
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}