0

I have found a few post regarding this topic, but the code used in verbatim and slightly modified gives me an error. I have a .docx file named COPYING stored in a / prefix inside a resources folder. I would like to open this file with whatever default word editing application the system uses (most likely Word).

The code below gives me this error

ShellExecute 'file:///C:/Users/john/AppData/Local/Temp/COPYINGtemp.docx' failed (error 2).

` The issue may be that setFileName doesn't actually change location of the file? Or it seems as though the copy method doesn't actually do anything (when debugging I've looked inside the qrc directory and the temp directory and nothing is there). The copy bool results in false

QFile::copy(":/resources/COPYING.docx", ":/resources/COPYINGTemp.docx");
QFile GNULicenseCopyFile(":/resources/COPYINGtemp.docx");
const QString GNULicenseCopyDirectory = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
GNULicenseCopyFile.setFileName(GNULicenseCopyDirectory + "/COPYINGtemp.docx");
QDesktopServices::openUrl("file:///" + GNULicenseCopyDirectory + "/COPYINGtemp.docx");

the code below gives the same error

const QString GNULicenseCopyDirectory = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
QFile::copy(":/resources/Copying.docx", GNULicenseCopyDirectory + "/COPYINGtemp.docx");
QDesktopServices::openUrl("file:///" + GNULicenseCopyDirectory + "/COPYINGtemp.docx");
CrippledTable
  • 784
  • 5
  • 20
  • 1
    Qt's resource system lets you embed files in your application binary; there's no way for external applications that expect to work with a regular file system to know that they exist. You could copy the docx file somewhere else (say, the user's folder) and open it from there. – MrEricSir Aug 22 '17 at 03:01
  • How would I go about implementing this solution if the application is intended to be used on various third party computers. – CrippledTable Aug 22 '17 at 03:28
  • Ignoring the fact that various third party computers aren't guaranteed to have an application capable of displaying docx files, you can copy a file out of the resource with [QFile](doc.qt.io/qt-5/qfile.html)'s copy method, and locate a path to store the file with [QStandardPaths](http://doc.qt.io/qt-5/qstandardpaths.html). `QStandardPaths::TempLocation` might be a good option if you don't need the file to persist. – MrEricSir Aug 22 '17 at 03:38
  • @MrEricSir I edited my code to include your suggestion the new bit of code is the first one in my question now. Still having issus however – CrippledTable Aug 22 '17 at 16:54
  • Do Qt applications have permission to copy files into the user's temporary folder? – CrippledTable Aug 22 '17 at 19:10

0 Answers0