I have this problem when I try to save a file in my GUI with qfiledialog. I don't know why, but as I click on the save_button widget, the file dialog opens, I type in a file name and click save, but my variable saveFileName remains empty after the process, so my program always stops there. I do not click on cancel! I really click on save, so how can getSaveFileName() return an empty string? Thank you!!
void MainWindow::on_save_button_clicked()
{
QString jsonFilter = "JSON Files (*.json)";
QString saveFileName = QFileDialog::getSaveFileName(this, "Save file", QDir::homePath(), jsonFilter);
if(fileName.isEmpty())
{
qDebug() << "no save file";
}
else
{
QJsonDocument jsonSaveDoc;
QJsonObject jsonSaveObj; // else statement continues if fileName contains something
You aren't checking the correct variable's value. Maybe it is just a typo, but it should be saveFileName
instead of fileName
. Try something like this:
void MainWindow::on_save_button_clicked()
{
QString jsonFilter = "JSON Files (*.json)";
QString saveFileName = QFileDialog::getSaveFileName(this, "Save file", QDir::homePath(), jsonFilter);
if(saveFileName.isEmpty())
{
qDebug() << "no save file";
}
else
{
QJsonDocument jsonSaveDoc;
QJsonObject jsonSaveObj; // else statement continues if saveFileName contains something
}
}