Save a .rtf file formatting the text in a QTextEdit
-
wrote on 1 Aug 2021, 12:28 last edited by HenkCoder 8 Jan 2021, 12:34
I'm currently making a notepad and I've found a problem. I want to save a file formatting the text so with different fonts, point sizes and colors in a .rtf file, but it just saves without formatting it so without any color or different fonts.
Here is the code I put to save a file:
void MainWindow::on_save_clicked() { QTextEdit *edit = getTabTextEdit(); QString fileName; if (currentFile.isEmpty()) { fileName = QFileDialog::getSaveFileName(this, tr("Save a file"), "New Document", tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)")); currentFile = fileName; } else { fileName = currentFile; } QFile file(fileName); if(ui->tabWidget->currentWidget() == ui->tab_1) { if(file.open(QIODevice::WriteOnly | QFile::Text)) { ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName()); QTextStream out (&file); out << ui->edit->toPlainText(); file.close(); ui->edit->setFocus(); } } else { if(file.open(QIODevice::WriteOnly | QFile::Text)) { ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName()); QTextStream out (&file); out << edit->toPlainText(); file.close(); edit->setFocus(); } } }
Is there a way to save a file formatting the text of the QTextEdit?
-
@artwaw
Oh, I really don't know how to do this, I don't know much about Qt, can someone give me some help?wrote on 1 Aug 2021, 13:35 last edited by@HenkCoder
Basically you can't, or it's hard. Why do you have to have.RTF
format anyway? Use HTML (Qt's "rich text"). -
I'm currently making a notepad and I've found a problem. I want to save a file formatting the text so with different fonts, point sizes and colors in a .rtf file, but it just saves without formatting it so without any color or different fonts.
Here is the code I put to save a file:
void MainWindow::on_save_clicked() { QTextEdit *edit = getTabTextEdit(); QString fileName; if (currentFile.isEmpty()) { fileName = QFileDialog::getSaveFileName(this, tr("Save a file"), "New Document", tr("Text File (*.txt);; RichTextFormat File (*.rtf);; All Files (*)")); currentFile = fileName; } else { fileName = currentFile; } QFile file(fileName); if(ui->tabWidget->currentWidget() == ui->tab_1) { if(file.open(QIODevice::WriteOnly | QFile::Text)) { ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName()); QTextStream out (&file); out << ui->edit->toPlainText(); file.close(); ui->edit->setFocus(); } } else { if(file.open(QIODevice::WriteOnly | QFile::Text)) { ui->tabWidget->setTabText(ui->tabWidget->currentIndex(), file.fileName()); QTextStream out (&file); out << edit->toPlainText(); file.close(); edit->setFocus(); } } }
Is there a way to save a file formatting the text of the QTextEdit?
wrote on 1 Aug 2021, 12:31 last edited by@HenkCoder There is, to some extent. Please see overview under the link below.
-
As already told on so you use toPlaintText() and wonder why the formatting is not saved... isn't the function name meaningful enough?
-
As already told on so you use toPlaintText() and wonder why the formatting is not saved... isn't the function name meaningful enough?
wrote on 1 Aug 2021, 12:39 last edited by@Christian-Ehrlicher alright, but is there a way to get the formatted text?
-
@HenkCoder There is, to some extent. Please see overview under the link below.
-
@artwaw Hey, I read the documentation and I wasn't able to find anything useful to my problem.
wrote on 1 Aug 2021, 13:03 last edited by@HenkCoder I suggest you read that again maybe?
As documentation states, rich text formatting in Qt is done via HTML subset. So, in case of QTextEdit, you will look at toHtml() method to obtain formatted text.
RTF is closed format, owned by Microsoft, however its specification is available - since Qt lacks direct support my approach would be to implement a model that converts Qt Rich Text to RTF and vice versa https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa140280(v=office.10)?redirectedfrom=MSDN
-
@HenkCoder I suggest you read that again maybe?
As documentation states, rich text formatting in Qt is done via HTML subset. So, in case of QTextEdit, you will look at toHtml() method to obtain formatted text.
RTF is closed format, owned by Microsoft, however its specification is available - since Qt lacks direct support my approach would be to implement a model that converts Qt Rich Text to RTF and vice versa https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa140280(v=office.10)?redirectedfrom=MSDN
-
@artwaw
Oh, I really don't know how to do this, I don't know much about Qt, can someone give me some help?wrote on 1 Aug 2021, 13:35 last edited by@HenkCoder
Basically you can't, or it's hard. Why do you have to have.RTF
format anyway? Use HTML (Qt's "rich text"). -
@HenkCoder
Basically you can't, or it's hard. Why do you have to have.RTF
format anyway? Use HTML (Qt's "rich text"). -
@JonB because I want to save a file, I can't let the user save an HTML file, he may not understand it
wrote on 1 Aug 2021, 17:07 last edited by@HenkCoder
RTF is a Microsoft format, effectively. " he may not understand it" --- "he" who/what? It's an "unusual" format to want to save in. Anyway, the answer remains the same. -
@HenkCoder
RTF is a Microsoft format, effectively. " he may not understand it" --- "he" who/what? It's an "unusual" format to want to save in. Anyway, the answer remains the same. -
wrote on 1 Aug 2021, 17:30 last edited by
@HenkCoder oh nevermind, I managed to understand. Thank you for your help.
1/12