How to delete a file within thread using

  • I try to delete a file, but i get error it's using by other progressing. Specially, I use a thread to copy content file but during copying, i want to crop down copy progressing and delete the file not intact.
    How to disconect that progress to file deleted.


  • Moderators

    if the file is opened by your application you need to make sure that you call QFile::close() on it.
    If the file is locked by another process/application there is nothing you can/should do.

  • I try to use:
    but not working.
    Get a error:
    @error: C2352: 'QFile::close' : illegal call of non-static member function@

  • Moderators

    i don't believe that this is the actual code you are using!
    Why don't you post the correct code when you expect us to help you?!

    I assume you call a member of type QFile out of a static method, right?

  • I'm really sorry you,
    I get error above when i use:
    in my class. So, I try other away,
    @ QFile("C:/abc.mp3").close();
    But also file not deleted.

  • That right,
    @void MainWindow::on_pushButton_clicked()
    QFile("c:/des/1 (1).iso").close();
    this->destination->remove("c:/des/1 (1).iso");

    destination is variable QDir type

  • Moderators

    QFile::close() is not a static function so remove this line and use it like in line 4.
    QFile file("c:/des/1 (1).iso");
    qDebug() << "QFile::remove()" << file.remove();
    qDebug() << "QFile::errorString()" << file.errorString();
    What are the outputs of this code?

  • Hi, outputs as below:
    @QFile::remove() false
    QFile::errorString() "The process cannot access the file because it is being used by another process.@

  • Moderators

    you can use "ProcessExplorer": and check which process is holding the handle to that file.
    If it's your application check your code again where you "open": it and make sure you close it properly. If it's another application there is nothing you can do...

  • Thank you very much,
    I understand you said.
    Let me see again.
    By the way, Have you a simple example about postEvent() function to me clear it?

  • Moderators

    [quote author="phamvanan" date="1371204058"]
    By the way, Have you a simple example about postEvent() function to me clear it?[/quote]
    "Here": you go.

  • I have a problem. I have a two objects, one is Copier object and else MainWindow Object,
    I want create postEvent at Copier class to MainWindow class.
    How to do that? At MainWindow how to get that event.
    Addition, How to send a data with postEvent function?

  • Moderators

    open the link i've posted before and just scroll down a little bit.... :/

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.