P
P
Pavel Matveev2019-11-01 21:12:57
Qt
Pavel Matveev, 2019-11-01 21:12:57

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

1 answer(s)
G
Grigory, 2019-11-01
@axe_lankaster13

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 question

Ask a Question

731 491 924 answers to any question