Answer the question
In order to leave comments, you need to log in
Qt program crashes when unsaving a file?
I am writing a program for coursework in C++ with Qt. Added file saving via QFileDialog::getSaveFileName. When you click cancel on the dialog box, the whole program crashes. And if you still save the file, everything works fine. I suspect that you need to write if to cancel saving, but I did not find methods / signals for tracking the event of clicking on the "Cancel" button in the dialog box.
A procedure that invokes a dialog box for saving a table to a file. The procedure gets a table from a QTabWidget.
QString Widget::topMenuSaveHowFunct(QTableWidget *table, qint16 width, qint16 height)
{
QString strFilter;
QString name = workPlace.tabText(workPlace.currentIndex());
QFile file(name + ".jot");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QString str = QFileDialog::getSaveFileName(nullptr,tr("Сохранить как..."),
name,
"*.jot",
&strFilter);
if(!strFilter.contains("jot"))
{
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
QTextStream writeStream(&file);
writeStream << i << "," << j << "," << table->takeItem(i,j) << ";\n";
}
}
}
file.close();
return strFilter;
}
Answer the question
In order to leave comments, you need to log in
Dialog is not being used correctly. What is the point of creating a file with a known name, and then calling a dialog to select this name. The bottom line is that the name selected in the dialog is returned to the str. If you press cancel in the dialog, the line will be empty, otherwise there will be a file name to save. And already with this name open the file for writing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question