Code freezes on trying to open QDialog - c++

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.

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!

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.

Related

QFileDialog::getOpenFileName poor performance and user experience

I want to let my users select an existing file from the filesystem. Looks like Qt's solution is QFileDialog. I looked up the docs and came up with something like this:
void MainWindow::openFileDialog()
{
const QString& inifilePath =
QFileDialog::getOpenFileName(this,
tr("Select Ini file"),
QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),
tr("Ini files (*.ini)"));
qDebug() << inifilePath;
}
MainWindow is my main window class. The problem is that file dialog window opens unacceptably slowly. After that it is still glitchy. If you right-click on something on the side bar, it again takes forever to show the rightclick options and you get a runtime warning-like output:
***********Create CommApi2Trayhelper************
***********Destruct CommApi2TrayHelper************
**********CommApi2TrayHelper::UnInitCheck*************
When the user selects some file and clicks OK program gives another runtime warning:
shell\comdlg32\fileopensave.cpp(14403)\comdlg32.dll!00007FFD757795AE: (caller: 00007FFD757A8291) ReturnHr(1) tid(1d9c) 80004005 Unspecified error CallContext:[\PickerModalLoop]
All these lead me to believe that I am doing something wrong, even though I am doing exactly what the examples in the docs are doing. What am I doing wrong?

Qt::QFileDialog crashes my application when called a second time

I am very new to Qt and OpenCV and am creating a project integrating both. The problem I am running into is that I have a button to load a file, which uses QFileDialog. The whole thing runs smoothly and my file gets loaded. However, it crashes if I click the load button a second time. It seems like the problem occurs at the call to QFileDialog::getOpenFileName, but I need an expert opinion.
This is the function for the button click.
void MainWindow::on_pushButton_clicked()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
// dialog.setAttribute(Qt::WA_DeleteOnClose);
// dialog.DontUseNativeDialog;
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"));
imageObject = new QImage();
imageObject->load(filename);
image = QPixmap::fromImage(*imageObject);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->graphicsView->setScene(scene);
ui->graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
cvHandler = new OpenCVHandler(filename.toStdString());
}
I have already tried both the lines that are commented out. My search also turned up nothing that I could understand easily:
Crash when calling getOpenFileName from QItemDelegate's custom editor
QFileDialog opens a second (possibly parent) unwanted window
Qt File Dialog Rendered Incorrectly and Crashes
If at all relevant, I am on an Ubuntu 16.04 LTS system.
Thank you
The problem was in the commented lines. I didn't use dialog.DontUseNativeDialog properly. Using it inside the getOpenFileName function did the trick:
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Images (*.png *.xpm *.jpg)"),0,QFileDialog::DontUseNativeDialog);
Thank you all.

OpenFileDialog freez in QT creator

When I open an open file dialog the first time everything works as it should, meaning the dialog opens and I can select a certain file. The problem I have is that each time I am trying to open an open file dialog a second time my whole program freezes. I have no idea why this is happening. I have debugged my program and I've seen that each time the program freezes at the dialog.exec() method. This issue started after I have upgraded my qt creator to version 5.8. I have also tried deleting the dialog instance each time I press a button and creating a new one, the result was the same. I include the header #include < QFileDialog >. Bellow you have my code posted.
QFileDialog *dialog;
dialog = new QFileDialog(this);
dialog->setFileMode(QFileDialog::AnyFile);
dialog->setNameFilter(tr("Images (*.png *.xpm *.jpg *.bmp)"));
QStringList l;
QString file;
if (dialog->exec())
{
l = dialog->selectedFiles();
file = l.at(0);
//do things with the file
QMessageBox::information(this,"Image Loader","Image loaded successful");
}
If anyone knows how to solve this I would be grateful.

QFileDialog causes my application to become less responsive

Firstly, for context, I am connecting a signal from triggered from QAction to a slot called fileOpen in this, and other similar connections are done in a method in my main window class like the following:
void MainWindow::createActions()
{
m_fileNew = new QAction("&New", this);
m_fileOpen = new QAction("&Open", this);
m_fileExit = new QAction("E&xit", this);
connect(m_fileNew, SIGNAL(triggered(bool)), this, SLOT(fileNew()));
connect(m_fileOpen, SIGNAL(triggered(bool)), this, SLOT(fileOpen()));
connect(m_fileExit, SIGNAL(triggered(bool)), this, SLOT(fileExit()));
}
To show the file dialog, the static method QFileDialog::getOpenFileName is used in MainWindow::fileOpen:
void MainWindow::fileOpen()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open Audio File"),
"", tr("WAVE Files (*.wav);;All Files (*.*)"));
if (filename != QString::null) {
m_fileName = filename;
}
}
The signal-slot connection between m_fileOpen and fileOpen works and displays the file dialog, but after closing the dialog the window takes longer to redraw while resizing.
Why is that happening, and how can I fix it?
All I had to do was build my Qt application in release mode, which means removing the "CONFIG+=debug" argument from the qmake call.
In release mode the performance degradation is gone, which is great, although I don't understand what differences between the debug and release versions of the Qt libraries allow this to occur.

QFileDialog showing hidden files although system setting is off

I am using the following code to show an open dialog in Qt:
QString path = QFileDialog::getOpenFileName(this, tr("Open Config File"), QDir::rootPath(), "Text Files (*.txt *.csv *.*);;");
What I realised is that this dialog also shows hidden files although the system setting for showing hidden files is turned off. It's the same if I instantiate the QFileDialog manually and show it. I also couldn't find out how to turn this off via a filter.
Does anyone know if there is a way to achieve the desired behaviour?
Looks like there is no simple(by setting some flag) solution out there. So I recommend to use the filtering which is described in other SO answer.
But in your case you might use the following condition:
if(fileModel != nullptr)
{
QFileInfo info = fileModel->fileInfo(index0);
return info.isHidden();
}
return false;