Reputation: 133
I have a custom QDialog with a 'Save' button which is supposed to prompt the user with a QFileDialog and save the contents of a Table Widget to a file, but keep the dialog open.
This is the function that opens the dialog and saves the data:
bool ResultsDialog::saveData()
{
QString outfile = QFileDialog::getSaveFileName(this, tr("Save results"), tr(""), tr("CSV Files (*.csv)") );
if (outfile.isNull() || outfile.isEmpty() )
return false;
if (!CSVIO::write_to_csv(*ui->tableWidget, outfile) )
{
QMessageBox::critical(this, "Failed to save...", QString("Could not save file: %1").arg(outfile) );
return false;
}
return true;
}
This is the code in for the Save button slot:
void ResultsDialog::on_saveButton_clicked()
{
if (saveData() )
{
saved = true;
}
}
EDIT: This is the code that creates and opens the ResultsDialog:
void MainWindow::displayResults()
{
ResultsDialog *resultsDialog = new ResultsDialog(this);
resultsDialog->setWindowTitle(windowTitle() );
resultsDialog->setWindowIcon(windowIcon() );
connect(resultsDialog, &ResultsDialog::finished, this, &MainWindow::close);
resultsDialog->fill(playlistTable, notes);
resultsDialog->setModal(true);
resultsDialog->open();
this->hide();
}
The problem is, as soon as the QFileDialog is closed, my QDialog is closed along with it, and the following message is printed on the console:
qt.qpa.xcb: QXcbConnection: XCB error: 3 (BadWindow), sequence: 13049, resource id: 25205189, major code: 40 (TranslateCoords), minor code: 0
11:59:01: /home/user1/workspace/build-App-Desktop-Debug/App exited with code 0
Note that the MainWindow, which is the parent of my custom dialog is hidden. When it's not hidden, this problem goes away, but that's not an option as I want the MainWindow to be hidden.
I am using Qt 5.15 on Debian Bullseye.
Upvotes: 0
Views: 345
Reputation: 6329
You are hooking the termination of the MainWindow to a signal that is sent out while ResultsDialog is still processing. ResultsDialog has MainWindow as its parent so the parent (and its children!!) are destroyed while the child is still running. Nothing good will arise from this construct.
Edit: The Error message you see is not the source of the issue but just an indication that a window was closed suddenly. Which is exactly what you describe.
Edit 2: See the comment section for a workaround (passing 0 as parent to the dialog) and some guessing why the workaround works.
Upvotes: 1