Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Issue with QTemporaryFile() class in Windows



  • Hi all,
    My system is Windows7 32-bit with Qt SDK 4.8.4 with VS2010 and MinGW compilers. Qt Creator 2.4.1
    I have a application where I am getting a list of files from server in a QTreeWidget and I have the option of Downloading as well as well Open the file from the list.
    In case of 'Open' action, the file is downloaded through HTTP in a temp location and it is opened through QDesktopServices::openUrl() functionality.
    Now I use the following code for this:
    @
    if (open)
    file = new QTemporaryFile(QDir::tempPath() + "/XXXXXX." + fileName);
    @

    After file is downloaded, open action is triggered as below:
    @
    file->close();
    if(file->isOpen())
    qDebug() << "File still opened.";
    if (open)
    QDesktopServices::openUrl("file:///" + file->fileName());
    @

    With this code, the file is getting downloaded properly in Temporary path(C:\Users\James\AppData\Local\Temp) but it is not getting opened through QDesktopServices. It is giving error that file is currently opened with another program. On further debugging, I found the application itself is having a hold of that downloaded file. The file is getting closed properly with file->close(). Another observation is it opens small files... like upto 10KB text files.

    Now, if I replace the replace the above code with QDesktopServices::storageLocation(), it works perfectly. See below code:
    @
    if (open)
    {
    file = new QFile(QDesktopServices::storageLocation(QDesktopServices::TempLocation) + "/" + fileName);
    }
    @

    Is this an issue with QTemporaryFile class? Need help seriously.

    Regards,
    Girish


Log in to reply