Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt5 QDir rename returning false

Tags:

c++

qt5

I am new to Qt and learning about renaming files with the QDir and QFile libraries.

I know how to rename and I can do it but when I try in this loop it does not work.

QDir editFile;
std::cout << "Attempting to chop and put .mp4 onto a regular file.\n";
QString fileNameBuf{argv};
QString fileOriginal{argv};
for(int l{0}; l < fileNameBuf.size(); ++l)
{
    if(fileNameBuf.at(l) == '.')
    {
        fileNameBuf.chop(fileNameBuf.size() - l);
        break;
    }
}
fileNameBuf.append(".mp4");
if(editFile.rename(argv, fileNameBuf))
    std::cout << "Successful\n";
else
    std::cout << "did not make it.\n";

This is actually in a function that sends a QString named argv. argv is a file path. I have it append .mp4 and remove the old extension. I have heard and know that if you do Q rename wrong it will not work. Please help me with this code. I have looked at other posts about this but they do not seem to help.

like image 292
Jake Avatar asked Jan 21 '26 18:01

Jake


1 Answers

I hope you are aware that files don't work like that... just changing the extension of a file doesn't mean it result into a new valid file type.

having said that. No need to reinvent the wheel:

check the official doc:

https://doc.qt.io/qt-5/qfile.html#rename-1

QFile editFile{"foo.txt"};
std::cout << "Attempting to chop and put .mp4 onto a regular file.\n";
if(editFile.rename("foo.mp4"))
    std::cout << "Successful\n";
else
    std::cout << "did not make it.\n";
like image 76
ΦXocę 웃 Пepeúpa ツ Avatar answered Jan 24 '26 08:01

ΦXocę 웃 Пepeúpa ツ