QT-File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
file = new QFile();

int Editor::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
this->file->setFileName(fileName);
this->fileInfo = new QFileInfo(fileName);
// qDebug()<<editor->fileInfo->fileName()<<editor->fileInfo->absolutePath()<<editor->fileInfo->baseName();

if(this->file->open(QIODevice::ReadOnly))
{

QTextStream in(this->file);
this->setPlainText(in.readAll());
// qDebug()<<editor->file->fileName();
this->file->close();
return 1;
}
else
{
qDebug()<<"Open failed";
return 0;
}
}

int Editor::saveFile()
{
QString fileName;
if(!this->file->exists())
{
fileName = QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath());
this->file->setFileName(fileName);
this->fileInfo = new QFileInfo(fileName);
}

if(this->file->open(QIODevice::WriteOnly))
{
QTextStream out(this->file);
out<<this->toPlainText();
this->file->close();
this->isSaved = true;
return 1;
}
else {
qDebug() << "Save failed";
return 0;
}
}