将 QPixmap 保存为 JPEG 失败(Qt 4.5)

发布于 2024-07-27 23:22:30 字数 1122 浏览 6 评论 0原文

我有以下代码。

QString fileName = QFileDialog::getSaveFileName(
   this, 
   tr("Output Image file"),
   (""),
   tr("PNG (*.png);;JPEG (*.JPEG);;Windows Bitmap (*.bmp);;All Files (*.*)")
);

if(fileName != "")
{
   QwtPlot* pPlot = ...
   QSize size = pPlot->size();
   QRect printingRect(QPoint(0, 0), size);

   QPixmap pixmapPrinter(size);
   pixmapPrinter.fill(Qt::white);

   {
      QPainter painter(&pixmapPrinter); 
      pPlot->print(&painter, printingRect);     
   } 

   bool isOk = pixmapPrinter.save(fileName);

   if(!isOk)
   {                
      QString msgText = tr("Failed to write into ") + fileName;

      QMessageBox::critical(this, tr("Error Writing"), msgText);
   }
}

所以,路径是这样的: - 弹出文件对话框 - 用户选择格式和文件 - 系统将绘图绘制到 QPixmap 上 - 将 QPixmap 保存到文件中。

它适用于 PNG 和 BMP 没有问题,但对于 JPEG、jpg、JPG 等则失败。

我翻遍了 Qt 文档,但找不到任何细节。 它应该可以工作。 有任何想法吗?

我使用的是 Qt 商业版,4.5.1 for Windows。
我使用的是 dll,Qt 不在路径上。

我刚刚意识到我正在静态链接到经典的第 3 方 jpeg.lib(独立 JPEG 组织的 JPEG 软件),该软件由其他库使用。

会不会因此而产生冲突或者什么事情?

或者只是插件没有正确加载。

I have the following code.

QString fileName = QFileDialog::getSaveFileName(
   this, 
   tr("Output Image file"),
   (""),
   tr("PNG (*.png);;JPEG (*.JPEG);;Windows Bitmap (*.bmp);;All Files (*.*)")
);

if(fileName != "")
{
   QwtPlot* pPlot = ...
   QSize size = pPlot->size();
   QRect printingRect(QPoint(0, 0), size);

   QPixmap pixmapPrinter(size);
   pixmapPrinter.fill(Qt::white);

   {
      QPainter painter(&pixmapPrinter); 
      pPlot->print(&painter, printingRect);     
   } 

   bool isOk = pixmapPrinter.save(fileName);

   if(!isOk)
   {                
      QString msgText = tr("Failed to write into ") + fileName;

      QMessageBox::critical(this, tr("Error Writing"), msgText);
   }
}

So, the path is like this: - File dialog pops up - users selects format and file - the system draws plot onto QPixmap - Saves QPixmap into the file.

It works for PNG and BMP without a problem, but for JPEG, jpg, JPG, etc it fails.

I was all over Qt documentation but could not find any details. It should just work.
Any ideas?

I am using Qt commercial edition, 4.5.1 for Windows.
I am using dlls, Qt is not on the path.

I just realised that I am linking statically to a classical 3rd party jpeg.lib (The Independent JPEG Group's JPEG software), which is used by other library.

Is it possible that a conflict or something arises because of this?

Or it is simply that plugin is not loaded properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Hello爱情风 2024-08-03 23:22:30

可能它找不到插件...

您可以将库路径添加到项目中,或者您可以简单地将 imageformats 文件夹放在二进制文件附近。

imageformats 文件夹位于插件中..

(可能您也无法显示 jpeg 图像)

probably it cant find the plugin...

you can add library path to project or you can simply put imageformats folder near your binary.

imageformats folder is in plugins..

(probably you cant display jpeg images too)

最美不过初阳 2024-08-03 23:22:30

如果您要进行静态构建,则必须将 QTPLUGIN += qjpeg 添加到 .pro 文件中,以便将图像格式的静态 jpeg 库链接到您的应用程序。

If you are making a static build, you have to add QTPLUGIN += qjpeg to your .pro file, so that the static jpeg library of the imageformats is linked with your application.

孤千羽 2024-08-03 23:22:30

您的插件很可能丢失,最好的工作方法是仅列出工具包支持的图像格式。

此示例来自我的插入图片,但您应该能够对其进行调整以保存为:

QString fileFormats = "(";
/* Get all inputformats */
for (int i = 0; i < QImageReader::supportedImageFormats().count(); i++) {
    fileFormats += "*."; /* Insert wildcard */
    fileFormats
            += QString(QImageReader::supportedImageFormats().at(i)).toLower(); /* Insert the format */
    fileFormats += " "; /* Insert a space */
}
fileFormats += ")";

QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
        currentPath, tr("Images ") + fileFormats);

此外,如果开发人员将调试版本复制到 QA 机器,我们有时会丢失格式。 调试版本将寻找调试插件,但无法加载它们。

Your plugin is most likely missing, best way to work is by only listing image formats that the toolkit supports.

This example is from my insert picture but you should be able to adapt it for your save as:

QString fileFormats = "(";
/* Get all inputformats */
for (int i = 0; i < QImageReader::supportedImageFormats().count(); i++) {
    fileFormats += "*."; /* Insert wildcard */
    fileFormats
            += QString(QImageReader::supportedImageFormats().at(i)).toLower(); /* Insert the format */
    fileFormats += " "; /* Insert a space */
}
fileFormats += ")";

QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
        currentPath, tr("Images ") + fileFormats);

Also we sometimes lose formats if a developer copies a debug build to a QA machine. The Debug version will be looking for the debug plugins and fail to load them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文