Code freezes on trying to open QDialog

混江龙づ霸主 提交于 2021-02-07 10:28:41

问题


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.


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/31983412/code-freezes-on-trying-to-open-qdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!