QT图片操作(复制 重命名 删除)

1.删除指定路径的文件.

//删除文件.返回一个bool值,判断是否成功删除,如果路径不存在这些,则返回false.
QFile file("c:/users/administrator/desktop...");
bool ok = file.remove();
//因为QFile::remove()是个静态方法,可以直接调用.
bool ok = QFile::remove("c:/users/administrator/desktop...");

2.给指定文件重命名

//
QFile file("c:/users/administrator/desktop...");
bool ok = file.rename("...");
//因为QFile::rename()也是个静态方法,可以直接调用.返回值非常重要,是用来判断是否成功操作的.
bool ok = QFile::remove("c:/users/administrator/desktop...","c:/users/administrator/desktop...");

3.判断指定文件是否存在.

//判断指定文件是否存在.也可以判断在指定路径下是否有文件和你将要保存的文件重名.
bool exist = QFile::exists("c/users/administrator/desktop/2.png");

4.获取用户对指定文件的权限,是否可读,可写这些.

QFlags<QFileDevice::Permission> power = QFile::permissions("c:/users/administrator/desktop/2.png");
//判断文件所有者是否可读.
if (power.testFlag(QFile::ReadOwner))
        qDebug("Can Read!");

5.拷贝文件.

//把桌面上的图片2拷贝一份,命名为10.
bool ok = QFile::copy("c:/users/administrator/desktop/2.png", "c:/users/administrator/desktop/10.png");

6.打开文件.

QFile file("c:/users/administrator/desktop/2.txt");
//以只读的方式打开文件.
bool ok = file.open(QIODevice::ReadOnly);
Qt 中,可以使用QFile类来复制重命名文件,使用QMessageBox类来显示警告或确认消息框。以下是一个示例代码,演示如何复制重命名文件,并在需要时替换现有文件: ```c++ #include <QFile> #include <QMessageBox> // 复制文件 bool copyFile(const QString& sourceFile, const QString& destinationFile) { QFile source(sourceFile); QFile destination(destinationFile); // 如果目标文件已经存在,提示用户是否要替换 if (destination.exists()) { QMessageBox::StandardButton reply; reply = QMessageBox::question(nullptr, "File already exists", "Do you want to replace?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::No) return false; else destination.remove(); // 删除现有的文件 } // 打开源文件和目标文件 if (source.open(QIODevice::ReadOnly) && destination.open(QIODevice::WriteOnly)) { // 复制文件内容 destination.write(source.readAll()); // 关闭文件 source.close(); destination.close(); return true; } else { // 显示错误消息 QMessageBox::critical(nullptr, "Error", "Unable to copy file."); return false; } } // 重命名文件 bool renameFile(const QString& sourceFile, const QString& destinationFile) { // 如果目标文件已经存在,提示用户是否要替换 if (QFile::exists(destinationFile)) { QMessageBox::StandardButton reply; reply = QMessageBox::question(nullptr, "File already exists", "Do you want to replace?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::No) return false; else QFile::remove(destinationFile); // 删除现有的文件 } // 重命名文件 if (QFile::rename(sourceFile, destinationFile)) return true; else { // 显示错误消息 QMessageBox::critical(nullptr, "Error", "Unable to rename file."); return false; } } ``` 使用时,可以像这样调用复制文件和重命名文件的函数: ```c++ QString sourceFile = "path/to/source/file"; QString destinationFile = "path/to/destination/file"; // 复制文件 if (copyFile(sourceFile, destinationFile)) qDebug() << "File copied successfully."; // 重命名文件 if (renameFile(sourceFile, destinationFile)) qDebug() << "File renamed successfully."; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值