PDA

View Full Version : QFileDialog prevents app from closing



hersheyzombie
14th July 2009, 17:27
Using QFileDialog::getOpenFileName(...) anywhere in my mainWindow prevents the application from closing when the mainWindow is ultimately closed; it continues to run in the background until I stop it through task manager or, if running in Qt Creator, click the red square.

QColorDialog and QMessageBox do not cause this problem, so it is not merely caused by opening a new window.

I have tried connecting both the application's lastWindowClosed() signal and the mainWindow's destroyed() signal to the application's quit() slot, to no effect.

I defined a destructor for my mainWindow and verified that the code there executes when the user clicks the red X in the corner. I even put the command this->destroy() in that destructor, again to no effect.

I would appreciate advice if anyone has encountered this or a similar issue.

hersheyzombie
14th July 2009, 23:04
A bizarre twist: I found that the problem goes away if I remove from the project a small file for converting between QImages and IPLImages (used in OpenCV)

The file, which i found online, does not do anything that seems like it should be relevant to QFileDialog, here is the header:


#ifndef QTIPL_H
#define QTIPL_H
#include <QImage>
#include <highgui.h>

IplImage qtToCv(QImage* qImage);
QImage cvToQt(IplImage* iplImage);

#endif // QTIPL_H



and cpp:

#include "qtIPL.h"

IplImage qtToCv(QImage* qImage)
{
IplImage* cvImage;
cvImage = cvCreateImageHeader(cvSize(qImage->width(), qImage->height()), IPL_DEPTH_8U, 4);
cvImage->imageData = (char*)qImage->bits();
IplImage* colorImage = cvCreateImage( cvGetSize(cvImage), 8, 3 );
cvConvertImage( cvImage, colorImage, 0 );

return *colorImage;
}

QImage cvToQt(IplImage* iplImage)
{
if(!cvSaveImage("tmpImg.bmp", iplImage)) printf("Could not save: tmpImg.bmp in cvToQt");
QImage img("tmpImg.bmp");
}



if anyone sees something wrong with this (or can recommend a similar way to convert between these two image formats) please let me know

PanuR
15th July 2009, 09:12
Hi

I am having this same problem at the moment also, and I am also using OpenCV. It is indeed bizarre that the problem could come from something that has nothing to do with QFileDialog. Not sure if this has anything to do with it, but is your app multithreaded? (Mine is.)

hersheyzombie
15th July 2009, 19:20
No, no multithreading (unless Qt does some behind the scenes for event listeners and such)

PanuR
17th July 2009, 11:27
I managed to narrow down the problem. There seems to be some incompatibility with the highgui and/or cxcore libraries from Opencv and the QFileDialog::getOpenFileName function (and who knows, possibly some others too).

I attached the qtIPL files you posted to a test project and was able to reproduce the problem. I did not even have to include the files in code, just having the files in the project was enough to cause the problem. Something is happening when the compiler is linking these libraries which causes the problem. Removing the files from the project and rebuilding removes the problem. I am using Visual Studio 2008. Any ideas are welcome.

PanuR
17th July 2009, 11:56
It seems the highgui library is the problem. I modified the qtIPL code to only use the cxcore library and the problem went away. This I think solves your problem, but I'm using too many highgui functions to get rid of them so it doesn't really help me. So I guess then that when integrating OpenCV with Qt one should stay away from the highgui functions. Here is the modified code:


#ifndef QTIPL_H
#define QTIPL_H
#include <QImage>
#include <cxcore.h>

IplImage qtToCv(QImage* qImage);
QImage cvToQt(IplImage* iplImage);

#endif // QTIPL_H

#include "qtIPL.h"

IplImage qtToCv(QImage* qImage)
{
IplImage* cvImage;
cvImage = cvCreateImageHeader(cvSize(qImage->width(), qImage->height()), IPL_DEPTH_8U, 4);
cvImage->imageData = (char*)qImage->bits();
IplImage* colorImage = cvCreateImage( cvGetSize(cvImage), 8, 3 );
cvCopy( cvImage, colorImage ); //cvConvertImage was just doing a copy

return *colorImage;
}

QImage cvToQt(IplImage* iplImage)
{
cvSave( "tmpImg.bmp", iplImage );
//do something to check if succesful
QImage img("tmpImg.bmp");

return img;
}

PanuR
21st July 2009, 10:16
More info. It is actually only when using some Qt function that accesses the OS file system GUI/dialog (hmm, probably any OS GUI/dialog for that matter) that the Highgui library causes the program to hang when closing. I tried using QTreeView and QDirModel to work around the QFileDialog::getOpenFileName() function, but QDirModel also caused the same problem. But then I tried QTreeWidget and QDir to create the items and it worked. No more hang up when closing.