Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delete a QTemporaryFile which is in use by a different app
Forum Updated to NodeBB v4.3 + New Features

Delete a QTemporaryFile which is in use by a different app

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ProgSysP Offline
    ProgSysP Offline
    ProgSys
    wrote on last edited by ProgSys
    #1

    I wrote an application which displays the file contents of a custom archive in a QTreeView.
    The user can then double click a file to open it in a default application.

    I extract the file into a QTemporaryFile and run QDesktopServices::openUrl(QUrl(tempfile->fileName())) to open it.
    The problem is, when the application is closed while the temporary file is still open by the default application, it doesn't get deleted (tempfile->remove() returns false).

    Is there a way to force delete the file or is there maybe a better way doing this?

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QDesktopServices>
    #include <QUrl>
    #include <QDebug>
    #include <QByteArray>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_tempFile = new QTemporaryFile("tempXXXXXX.mp3", this);
        m_tempFile->setAutoRemove(true);
        m_tempFile->open();
    
        QFile testFile("your_song.mp3");
        testFile.open(QIODevice::ReadOnly);
        QByteArray fileData(testFile.readAll());
    
        QDataStream out(m_tempFile);
        out<<fileData;
        m_tempFile->close();
    
        qDebug()<<"Opening "<< m_tempFile->fileName();
        //is opned (in my case) by VLC
        QDesktopServices::openUrl(QUrl("file:///"+m_tempFile->fileName()));
    
    }
    
    MainWindow::~MainWindow()
    {
        // if app is closed tempXXXXXX.mp3 doesn't get deleted
        if(!m_tempFile->remove())
            qDebug()<<"Couldn't delete "<< m_tempFile->fileName();
    
        delete m_tempFile;
        delete ui;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you using Windows ? Depending on how the file is opened, you can't delete it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      ProgSysP 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        Are you using Windows ? Depending on how the file is opened, you can't delete it.

        ProgSysP Offline
        ProgSysP Offline
        ProgSys
        wrote on last edited by ProgSys
        #3

        @SGaist Yes, I am using Windows. Is there maybe a way to mark the files for deletion on reboot? So they at least get removed later.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          @ProgSys said:

          Is there maybe a way to mark the files for deletion on reboot? So they at least get removed later.

          AFAIK there is no such way,
          The best you can do - ask user if he wants to stop using file and delete it or maintain a list of remained to be deleted files and check it on next start-up and ask if user want to delete them.

          ProgSysP 1 Reply Last reply
          1
          • A alex_malyu

            @ProgSys said:

            Is there maybe a way to mark the files for deletion on reboot? So they at least get removed later.

            AFAIK there is no such way,
            The best you can do - ask user if he wants to stop using file and delete it or maintain a list of remained to be deleted files and check it on next start-up and ask if user want to delete them.

            ProgSysP Offline
            ProgSysP Offline
            ProgSys
            wrote on last edited by
            #5

            @alex_malyu Ok, I guess I'll just pop up some error messages.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved