You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, June 19th 2012, 12:49pm

New File

Hello,

I would like to create a new empty text file. The user should type in a name of the file and perhaps write in some text then press save to create a file at a given place.

I can write and read from a file. But I need some help with creating new files. I have tried to work with QTextDocumentWriter and QTextDocumentFragment, and it's not going so well.. I also wonder about setDevice, what should I set it to?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MainWindow::on_pushButton_clicked() {

QString fileName = ui->lineEdit->text();
QFile fil("C:/Users/" + fileName + ".txt");

QString writtenToFile = ui->textEdit->toPlainText();
QTextDocumentWriter writer(&fil, "plaintext");

writer.setFormat("txt");
writer.setFileName(fileName);
// writer.setDevice?

writer.write(QTextDocumentFragment::fromPlainText(writtenToFile));

}


I am very happy for all of your help.
//Jennifer

2

Tuesday, June 19th 2012, 3:22pm

"txt" is not a valid format - read this
http://doc.qt.nokia.com/4.7-snapshot/qte…DocumentFormats

Why instantiate QTextDocumentWriter with a QFile and then ALSO specify the filename?

"I also wonder about setDevice, what should I set it to?"
Nothing. Don't do anything with it.

Source code

1
2
3
4
5
QString filename = "C:/users/"
filename += ui->lineEdit->text();
filename += ".txt";
QTextDocumentWriter writer(filename);
writer.write(ui->textEdit->document());
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.