如何使用C++和Qt将QPixmap保存为文件夹中的图片

How to save a QPixmap as a picture in a folder with C++ and Qt?

本文关键字:文件夹 保存 QPixmap C++ 何使用 Qt      更新时间:2023-10-16

我正在尝试编写一个C++函数,使用Qt GUI将所选图片保存到我的程序目录中。

到目前为止,我在QPixmap对象上使用的"保存"函数不会保存任何内容,我也不知道为什么。

这是代码:

qImage = new QPixmap(path);
QPixmap qImage2 = qImage->scaled(this->width(),this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
qImage2.toImage();
qImage2.save(QDir::currentPath());
qDebug()<<QDir::currentPath();

有人能帮我吗?:)

  • QDir::currentPath()返回当前工作的目录。显然,没有指定文件名本身。只需附加所需的文件名,例如:QDir::currentPath() + "/123.png"

  • CCD_ 3是返回从CCD_ 5转换而来的CCD_。实际上,它在代码中没有任何用处,请删除它或使用QImage

  • QDir::currentPath()返回当前工作目录,该目录不必为应用程序可执行目录。如果需要可执行目录,请改用QCoreApplication::applicationDirPath()
  • 此外,正如Violet Giraffe所指出的,可能存在写入权限问题