OpenFileDialog freez in QT creator - c++

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.

Related

Why does the QMessageBox opened from QFileDialog::accept() does not stay on top on macOS?

I try to have with Qt a file dialog, which asks the user for confirmation if the chosen file does not exist. So basically the same you get with the Windows API via IFileDialog::SetOptions(FOS_CREATEPROMPT).
So I derived a new class from QFileDialog to overwrite QFileDialog::accept().
The new QFileDialogEx::accept() opens a QMessageBox when the user clicks on Open, but the file does not exist. If the user chooses "No", the file dialog will not be accepted and keeps open.
That works fine on Linux, but on macOS, the QMessageBox does not stay on top of the QFileDialog. So if the user clicks on the QFileDialog under the QMessageBox, it will come to the top, hiding the QMessageBox.
Here is my code:
class QFileDialogEx : public QFileDialog
{
public:
QFileDialogEx(QWidget *parent, const QString &caption, const QString &directory)
: QFileDialog(parent, caption, directory) {
this->setOption(QFileDialog::DontUseNativeDialog);
}
void accept(void) {
QString selectedFilePath = this->selectedFiles().value(0);
QFileInfo selectedFileInfo(selectedFilePath);
if (!selectedFileInfo.exists()) {
// File does not exist => Ask if file should be created
QMessageBox msgBox(this);
msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint);
msgBox.setText(selectedFileInfo.fileName() + "\nThis file doesn't exist.\n\nDo you want to create this file?");
msgBox.setWindowTitle("Create Prompt");
msgBox.setIcon(QMessageBox::Warning);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
if (msgBox.exec() == QMessageBox::No)
return; // Keep file dialog open
}
QFileDialog::accept();
}
};
I've searched a lot about keeping a QMessageBox box on top. Normally the point is: "Set the parent". But as you can see, I did that already. I also desperately tried Qt::WindowStaysOnTopHint and msgBox.setWindowModality(Qt::ApplicationModal) without success.
Just to be clear: I am not using the native file dialogs here. (QFileDialog::DontUseNativeDialogs)
Any idea how to let the QMessageBox stay on top of the QFileDialog also on macOS?
Or any other idea, on how to implement a prompt when the user clicks on Open, but the chosen file does not exist?

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?

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.

qt c++ QDialog open new file

After almost a week of searching and reading qt documentation, I still can't figure out how to use QDialog to create a NEW file on my hard disk for writing data. I can open a file and write data if the file already exists, but if I attempt to create a NEW file, I get a message that the file does not exist. I can create a new file if I do not use QDialog by hard coding the path and file name, but would like to be able to choose the file location, and get the customary messages; for instance that the file already exists and asked if it is OK to overwrite it. Here is a snippet of my latest attempt:
void MainWindow::on_pushButton_3_clicked()
{
QString filename = QFileDialog::getOpenFileName(
this,
tr("Sensor data"),
"C//",
"Text File (*.txt)"
);
QFile file(filename);
if (!file.open(QIODevice::ReadWrite))
{
QMessageBox::information(0,"info",file.errorString());
return;
}
QTextStream out(&file);
out<<"string1";
out<<"\n";
out<<"string2";
out<<"\n";
out<<"string3";
out<<"\n";
out<<"string4";
out<<"\n";
out<<"string5";
file.close();
}
Can QDialog be used for this purpose? If not, please point me to information on how it is done.
Thanks in advance!
I think you should use getSaveFileName instead
In Qt exemples there is project called SDI , content simple window and menu and all what you need about file : new, open, save, and save as .

Close QFileDialog only when click "open"

Whenever I select a file in my QFileDialog the accepted signal is fired and the window closes. I want to keep the window open so I can select multiple files and then capture the signal fired when "open" is clicked.
QFileDialog* myDialog = new QFileDialog(this);
myDialog->setFileMode(QFileDialog::ExistingFiles);
myDialog->setVisible(true);
What signals should I be connecting here to achieve this effect?
The QFileDialog::ExistingFiles should guarantee that multiple files can be selected. Given that, you can connect to the signal:
void QFileDialog::filesSelected(const QStringList & selected)
Directly from the documentation:
When the selection changes for local operations and the dialog is accepted, this signal is emitted with the (possibly empty) list of selected files.
However, if you are only interested in collecting such files, you can totally avoid signal-slot and write (taken again from the documentation):
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
Note that in this case dialog object has been created on the stack (which is the common approach for such objects).
Your code looks fine to me. I believe you are double clicking on the file inside the dialog instead of holding on the Ctrl and single clicking on all the files you need.
You can optionally use an event filter and ignore the double click event.
Once you click on Open, you can get a list of all the file paths in the QStringList given by QFileDialog::selectedFiles(). Also it's better to use a stack variable here and use exec method to launch it as pointed out by BaCaRoZzo.
QFileDialog myDialog(this);
myDialog.setFileMode(QFileDialog::ExistingFiles);
if(myDialog.exec())
{
qDebug() << myDialog.selectedFiles();
}
Whenever I select a file in my QFileDialog the accepted signal is fired and the window closes. I want to keep the window open so I can select multiple files
All other answers is just solution for selection many files one time and CLOSE window after Open button pressing. Get my solution, it is not very simple because it required lot of work:
I used lamda expressions and new signals and slots syntax in my answer, but you can use old syntax or add
CONFIG += c++11
to the .pro file and use lambdas.
Subclass QFileDialog:
Header:
#ifndef CUSTOMFILEDIALOG_H
#define CUSTOMFILEDIALOG_H
#include <QFileDialog>
#include <QDebug>
class CustomFileDialog : public QFileDialog
{
Q_OBJECT
public:
explicit CustomFileDialog(QWidget *parent = 0);
void setDefaultGeo(QRect);
signals:
void newPathAvailable(QStringList list);
public slots:
private:
bool openClicked;
QRect geo;
};
#endif // CUSTOMFILEDIALOG_H
When you click open, you hide your dialog, not close! Cpp:
#include "customfiledialog.h"
CustomFileDialog::CustomFileDialog(QWidget *parent) :
QFileDialog(parent)
{
openClicked = false;
connect(this,&QFileDialog::accepted,[=]() {
openClicked = true;
qDebug() << openClicked;
this->setGeometry(geo);
this->show();
emit newPathAvailable(this->selectedFiles());
});
}
void CustomFileDialog::setDefaultGeo(QRect rect)
{
geo = rect;
}
Usage:
CustomFileDialog *dialog = new CustomFileDialog;
QStringList fileNames;
dialog->setFileMode(QFileDialog::ExistingFiles);
dialog->show();
dialog->setDefaultGeo(dialog->geometry());
connect(dialog,&CustomFileDialog::newPathAvailable,[=](QStringList path) {
qDebug() << path;
});
Why do you need setDefaultGeo? Without this method, your window will move after Open pressing.
What we get?
I open filedialog and select two files:
I clicked Open, but window didn't close! You can choose new files again and again!
One more file and so on:
Window will closed only when user press Close button, but you will have all path which user choose.
As you said:
I want to keep the window open so I can select multiple files
You get this.
I don't think anyone has understood the question (or it could be just me looking for my own solution)...
I had the same issue. As soon as I clicked a file the dialog would close. I couldn't ever select a file and then click "Open" because the dialog instantly closed as soon as I single clicked a file.
related: qtcentre.org/threads/48782-QFileDialog-single-click-only
It turns out it was my linux os settings (under mouse). File opening was set to single-click. I still feel like something external might have toggled this but that is just speculation. It appears Qt was going the right thing. Check another application, like kate on KDE and see if it has the same behavior. That is what clued me in to the source of my issue.