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

QFile::remove works wrong or where is problem?



  • OS - Android
    I have database file which i can change on PC then copy to my phone.
    Program checks if there is a new one, copy it and then delete.

            bool copySuccess = QFile::copy(new_name, name);
            if (!copySuccess)
            {
                QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok);
            }
            else
            {
                QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok);
                QFile::remove(new_name);
            }
    

    if i run program again it says there isn't new database file but if i open it's location in file browser i can see it

    why is it so?


  • Moderators

    hi @DrageFabeldyr ,
    you can't overwrite existing files with QFile::copy

    taken from the docu:

    bool QFile::copy(const QString &fileName, const QString &newName)
    This is an overloaded function.
    
    Copies the file fileName to newName. Returns true if successful; otherwise returns false.
    
    If a file with the name newName already exists, copy() returns false (i.e., QFile will not overwrite it).
    
    See also rename().
    


  • @J.Hilk great thanks about copy, now i know what is wrong with it but my question was about remove

    if i remove file program can't see it after that but file browser can


  • Moderators

    @DrageFabeldyr
    mmh, interesting,

    Do you check if the Remove is successful? QFile::remove should return a bool. It won't work for example if the file can't be closed.



  • @J-Hilk
    QFile::remove returns true
    then program says there is no file
    file browser shows there is a file
    if i try to copy this file from the phone to pc there is no this file on pc after that so it looks like i'm trying to copy nothing
    if i try to copy file from pc to the phone to the same place it asks me if i want to overwrite the file so it looks like "nothing" really exist



  • If Qt's functions don't see the file, it doesn't exist. Your file browser "sees" it, but it may be caching the directory - try refreshing it and try to open it into a text editor or something.



  • @mvuori
    i've tried restart my phone, reconnect it to my pc and even connect it to another pc

    and i mean file browser on pc (Windows 7)
    file browser on the phone doesn't see the file (Android)


Log in to reply