我希望将文件从某个目录传输到另一个目录,但当我使用QDir时。重命名它似乎总是失败。
void handler::moveToTempFolder(QString localFilePath)
{
qDebug()<<localFilePath; <--- "C:/Users/user1/Pictures/IMG_00000009.jpg"
qDebug()<<"/TempFiles/" + getFileNameFromPath(localFilePath); <---- "/TempFiles/IMG_00000009.jpg" a folder that is in the same location as the .pro
QDir dir;
if(dir.rename(localFilePath,"/TempFiles/" + getFileNameFromPath(localFilePath)))
qDebug()<<"Success";
else
qDebug()<<"Failed";
}我的终端出故障了。有人能帮忙吗?
发布于 2019-02-09 22:41:54
Qt没有访问.pro文件所在的项目文件夹的方法。可能是因为一旦您部署了这个文件夹,除了您的设备之外,任何其他设备都不会有该文件夹。
但是,它可以使用QDir::currentPath()访问可执行文件所在的文件夹。
而且,我也不知道您是如何定义getFileNameFromPath的--但是您可以使用QFileInfo来实现这一点。
void MainWindow::moveToTempFolder(QString localFilePath)
{
QFileInfo fileInfo(localFilePath);
QDir dir;
QString tempFilePath = dir.currentPath() + "/TempFiles/" + fileInfo.fileName();
if(dir.rename(localFilePath, tempFilePath))
{
qDebug() << "Success";
} else {
qDebug() << "Failed";
}
}备注:当您从Qt运行此文件时,您的/文件/文件夹很可能与您的/debug/和/release/文件夹位于同一个文件夹中(与您的可执行文件相比提高一级)。但是,一旦您部署了这个文件(或者从.exe手动运行它)- /TempFiles/将与.exe放在同一个文件夹中。
发布于 2019-02-09 22:46:00
程序运行时的工作目录是构建文件夹,而不是项目文件夹。我猜那是你的问题。
https://stackoverflow.com/questions/54610507
复制相似问题