PDA

View Full Version : No file name specified



hacquerqop
20th July 2018, 11:34
I'm trying to open a file to my texteditor and I want to change and save it but it gives me that error
QFSFileEngine::open: No file name specified

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QFileDialog>
#include <QFile>
#include <QFileInfo>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionOpen_triggered()
{
QString file = QFileDialog::getOpenFileName(this); //this is open function

if(!file.isEmpty())
{

nFile.setFileName (file);
if(nFile.open (QFile::ReadOnly | QFile::Text))
{
nFile.fileName ()=file;
QTextStream in(&nFile);
QString text = in.readAll ();
nFile.close();

ui->textEdit->setPlainText (text);


}
}
}

void MainWindow::on_actionSave_triggered() //this is save function

{
//QString file; //I tried to fix the problem with these parts
//nFile.setFileName (file);
if(nFile.open (QFile::WriteOnly | QFile::Text))
{
QTextStream out(&nFile);
out << ui->textEdit->toPlainText ();

nFile.flush();
nFile.close();

}

d_stranz
20th July 2018, 16:56
Do you understand what line 32 is doing? If you think it is setting the name of the file used by the file engine, you need to review your C++ a bit more. The open() statement on line 49 is telling you exactly what is wrong: you haven't given any path to the file, so it has no clue which file you're trying to open.

In my opinion, you shouldn't even be using QFSFileEngine in the first place. You should be using one of the higher level classes, like QFile, to access the file system.