如何在Qt中复制图像文件?

def*_*ant 1 url copy file qt4

我需要将一些图像文件从给定位置复制到给定目录,我需要保留原始文件名.

它可以是url-> copy(img14,ImgPath);

其中img14是文件的路径,包括文件名,imgpath是目标目录.

img14可以是:/home/obscurant1st/Downloads/aaa.jpeg或C:\ abcd\asada.jpeg

Plase注意:我必须使用变量.我不能在代码中使用确切的路径!

小智 11

使用QFile :: copy.请注意,这两个参数都是完整文件路径,因此您需要使用目标目录和源文件名创建目标文件路径.并且QFileInfo :: fileName()可能会有用.

编辑:

只需创建一个获取源文件路径和目标目录的函数:

bool CopyFile(const QString& sourceFile, const QString& destinationDir)
{
    QFileInfo fileInfo(sourceFile);
    QString destinationFile = destinationDir + QDir::separator() + fileInfo.fileName();
    bool result = QFile::copy(sourceFile, destinationFile);
    return result;
}
Run Code Online (Sandbox Code Playgroud)