2009-07-17 49 views
3

我有以下代码。将QPixmap保存为JPEG失败(Qt 4.5)

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不在路径上。

我刚刚意识到我静态链接到一个经典的第三方jpeg.lib(独立JPEG组的JPEG软件),这是由其他库使用。

是否有可能因此而引起冲突或其他事情?

或者它只是简单的插件没有正确加载。

回答

5

也许它不能找到插件...

您可以添加到项目库路径,也可以简单地把imageformats文件夹中的二进制附近。

imageformats文件夹中的插件..

(可能你不能显示JPEG图像太)

+0

我认为最后我们设法将qt plugins目录中的imageformats文件夹放到项目目录中,然后在发布轨迹中进行排序。 – 2010-01-22 18:25:17

4

如果你是一个静态的构建,必须添加QTPLUGIN += qjpeg到您的.pro文件,从而使imageformats的静态jpeg库与您的应用程序链接。

+0

我没有进行静态构建(至少我认为)。 我的应用程序在没有QtCore4.dll和QtGui4.dll的情况下运行在与exe相同的目录中。 虽然 c:\ Qt \ 4.5.1 \ lib \ QtGui4.lib c:\ Qt \ 4.5.1 \ lib \ QtCore4.lib 提供给链接器,但我知道提供一个“桥”这样编译器就知道这个东西会在dll中出现,或者什么的。 我会尝试将QTPLUGIN + = qjpeg添加到.pro文件, 以查看它是否稍后会有所不同。 – 2009-07-17 17:46:57

4

你的插件很可能缺失,最好的工作方式是只列出工具包支持的图像格式。

这个例子是从我插入图片,但你应该能够适应它为你保存为:

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机。 Debug版本将寻找调试插件并且无法加载它们。