Unsolved Don't save files with changed font and color
-
Hi, I am creating an Notepad and I have problem with my color and font because when I changed color I can see it on textctrl but after saving and opening file I got only text ...
QColor color = QColorDialog::getColor(Qt::white,this,"Choose color"); if(color.isValid()) { ui->textEdit->setTextColor(color); }
-
@MorOver
Welcome to the forum!How do you save what to this "Notepad file"? What do you do about saving the color, and/or what you want this color applied to?
-
@JonB Hello :)
This is my save slotQString file = QFileDialog::getSaveFileName(this, "Save", QDir::homePath(),"Text file (*.txt);;C files (*.c);;C++ files (*.cpp);;Docx files (*.doc)"); QFile File(file); if(!Plik.open(QIODevice::WriteOnly)) { qDebug() << "error opening file: " << Plik.error(); return; } QString text; QTextStream out(&file); text = ui->textEdit->toPlainText(); out << text; file.flush(); file.close();
I just created simple Notepad app like open/save file and I want add color text and font size slots, one of these is above but where I saved my file to *.txt or *.doc extensions and open I see only text without any changes(I mean without font and color).
-
@MorOver said in Don't save files with changed font and color:
ui->textEdit->toPlainText();
You convert it to a plain text without any formatting information - how do you expect that a color or any other format can be restored afterwards? Take a look at toHtml() for example.
-
@MorOver
As @Christian-Ehrlicher has said.But you have a conceptual problem. Your proposed file types:
Text file (*.txt);;C files (*.c);;C++ files (*.cpp);;Docx files (*.doc)
First of all you should scrub
.doc
, you're not making any kind of Word doc file. But the rest are "plain text". And that would mean they can't be saved as HTML, and can't contain any saved color information. So you're going to have to think about what you actually want! -
@JonB So which extension file could save text with color and font?
-
@MorOver
It's not the extension per se, it's whether you want to be able to use it as a text file or not.For example, you just cannot put something to indicate a color or a font into a
.cpp
file which is to be used as C++ source for compiling.If you save from
QTextEdit::toHtml()
the only really correct thing is to save it as a.htm
/.html
file.If you look at, say, Microsoft Windows WordPad, which is an editor like yours in that you can save formatting --- and you cannot use it as a text or C++ file --- you'll see it saves as a
.doc
or a.rtf
file, and the content is indeed RTF, which is like HTML. -
@JonB Thank you for help ,It works when I changed method from toPlainText() to toHtml() and saved file in *.doc extension, I can open formatted text in my WordPad.
-
@MorOver
Good.But the
.doc
extension is a bit dubious. It's true that you can just save HTML to a.doc
and, say, WordPad or Word will be able to read it in OK. But the reverse is not necessarily true: save a.doc
from WordPad/Word and then try to read it back into yourQTextEdit
. It should not be good! BecauseQTextEdit
can't begin to cope with what can be in a.doc
.So be aware that if your user thinks they can read any
.doc
back into your editor they will be disappointed. They can only really read.doc
files you have created from your app. That's why.htm
would be a more accurate extension. Up to you.