tags:

views:

1344

answers:

3

Hello.

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.

+2  A: 

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)

ufukgun
i think at the end we manage to sort it out by putting the imageformats folder from qt plugins directory to the project directory, and then also in the release trajectory.
Denis C
+4  A: 

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.

alisami
I am not making a static build (at least that what I think).My application does not run without QtCore4.dll and QtGui4.dll in the same directory as the exe. Though c:\Qt\4.5.1\lib\QtGui4.libc:\Qt\4.5.1\lib\QtCore4.libare supplied to linker, but I understand it is standard to provide a "bridge" so that compiler knows that this things will come in dll, or something. I will try to add QTPLUGIN += qjpeg to .pro file,to see if it makes a different later.
Denis C
+3  A: 

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.

Phil Hannent