gaj
gaj

Reputation: 327

Code freezes on trying to open QDialog

I'm trying to debug a c++/Qt5.5 code in MSVS2010 Professional. A function has following lines of code,

/* Static method approach */
QString filters("Music files (*.mp3);;Text files (*.txt);;All files (*.*)");
QString defaultFilter("Text files (*.txt)");

QFileDialog::getSaveFileName(0, "Save file", QDir::currentPath(), filters, &defaultFilter);

The dialog is simply doesn't open and the application freezes.

I tried the alternative was as below.

/* Direct object construction approach */
    QFileDialog fileDialog(0, "Save file", QDir::currentPath(), filters);
    fileDialog.selectNameFilter(defaultFilter);
    fileDialog.exec();

But again, the code freezes at 'fileDialog.exec()'. So, I created a different new simple project with these statements only and the it worked as expected.

Is this a issue of my environment configuration. I tried to debug but on stepping into the above line simply freezes the code without any error.

Upvotes: 1

Views: 4449

Answers (2)

Tigzy
Tigzy

Reputation: 181

I had the same problem and discovered this could be because of a bad COM initialization in your UI thread. If you have somewhere:

HRESULT hres = CoInitializeEx( 0, COINIT_MULTITHREADED );

It MUST be replaced by:

HRESULT hres = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );

I think the native window is maybe using COM calls, and just sits here because of a deadlock.

Upvotes: 2

gaj
gaj

Reputation: 327

This looks like a know issue in Qt. https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-dialog/8

The workaround is to use QFileDialog::DontUseNativeDialog flag as below.

m_imageFile = QFileDialog::getOpenFileName(this, tr("Open Image"), QDir::homePath(), tr("Image Files (*.png *.jpg *.bmp)"), 0, QFileDialog::DontUseNativeDialog); //works

Thanks for the help though!

Upvotes: 4

Related Questions