I can not record .txt files with QFileDialog C ++ QT

3

This is my code:

void MainWindow::save(){
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save Text File"), path, tr("Text Files (*.txt)"));
    if (fileName != "")
    {
       QFileInfo info(fileName);
       path = info.absoluteDir().absolutePath();
       QFile file(path);
       if(!file.open(QIODevice::WriteOnly)){
           QString text = ui->plainTextEdit->toPlainText();
           QTextStream out(&file);
           out << text ;
           file.close();
       }
    }
}

After calling the method through a pushButton no file txt is created.

connect(ui->saveButton, SIGNAL(clicked(bool)), this, SLOT(save()));

This is the message that appears in the Log:

  

QIODevice :: write (QFile, "C: \ Users \ kfg \ Desktop"): device not open

    
asked by KFGC 25.02.2016 в 15:58
source

1 answer

1

My Solution:

void MainWindow::save()
{
    QString path = QDir::currentPath();
    QString filename = QFileDialog::getSaveFileName(this, tr("Save Text File"), path, tr("Text Files (*.txt)"));

    if(filename.isEmpty())
        return;

    QFile file(filename);

    if(!file.open(QFile::WriteOnly |
                  QFile::Text))
    {
        qDebug() << " Could not open file for writing";
        return;
    }

    QTextStream out(&file);
    out << ui->plainTextEdit->toPlainText();
    file.flush();
    file.close();
}
    
answered by 11.11.2016 в 23:59