简体   繁体   English

如何在Qt中移动文件?

[英]How to move files in Qt?

Qt中是否存在跨平台功能,相当于Windows中的MoveFile功能和Linux中的mv命令?

Sure, QDir::rename() following the old Unix / POSIX tradition of calling this rename. 当然, QDir::rename()遵循旧的Unix / POSIX调用此重命名的传统。

Which makes sense if you think of a file with its complete path: the underlying inodes just get assigned a different path/file label. 如果您考虑具有完整路径的文件,这是有意义的:基础inode 只是被分配了不同的路径/文件标签。

You would use QDir::rename() but be beware of the special cases when rename() can fail: 您可以使用QDir :: rename(),但要注意rename()失败时的特殊情况:

On most file systems, rename() fails only if oldName does not exist, if newName and oldName are not on the same partition or if a file with the new name already exists. 在大多数文件系统上,只有oldName不存在, newNameoldName不在同一分区上或者已存在具有新名称的文件时, rename()才会失败。 However, there are also other reasons why rename() can fail. 但是,还有其他原因导致rename()失败。 For example, on at least one file system rename() fails if newName points to an open file. 例如,如果newName指向打开的文件,则至少在一个文件系统上rename()失败。

QUrlOperator :: copy()是QDir :: rename()的替代品,也可能适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM