0

I am writing a text editor on qt and I stumbled with a problem of saving changes of the files. I use rich texedit to make changes with colors, but when I change colors of selected words and save file, it doesn't save color changes.

I know that I can make something like config.txt file, where app can automatically wright changes, but this will make global changes, and I want to save changes for every individual file.

Can it be done?

Remark: Word office can save individual changes so 1.doc and 2.doc differ by its formatting.

void MainWindow::on_actionFont_triggered()
{
    bool ok;

    QFont font = QFontDialog::getFont(&ok,QFont("Palatino Linotype",12,QFont::Normal),this);

    if(ok)
        ui->textEdit->setFont(font);
}

void MainWindow::on_actionText_Color_triggered()
{
    QColor color = QColorDialog::getColor(Qt::white,this);
    if(color.isValid())
        ui->textEdit->setTextColor(color);
}

void MainWindow::on_actionBackground_Color_triggered()
{
    QColor color = QColorDialog::getColor(Qt::white,this);
    QPalette palette;
    palette.setColor(QPalette::Base,color);
    if(color.isValid())
        ui->textEdit->setPalette(palette);
}
Giorgi
  • 169
  • 1
  • 2
  • 9
  • Please clarify your Question: What is the Qt Widget you are using? How do you apply color changes? How do you try to save your file? – Mailerdaimon Jul 16 '15 at 06:19
  • Regarding your remark: *.doc isn´t "just a file". You can unzip a *.doc and see that it is composed of several file. If you look further you see that things like formatting are saved in an xml structure. – Mailerdaimon Jul 16 '15 at 06:25
  • Settings the palette for chaning the text color is really not the way you should go. This is used to set the default color off all text in your widget. To color indiviual words you really should switch to html. See http://stackoverflow.com/q/2857864/2927205 – Mailerdaimon Jul 16 '15 at 08:21

1 Answers1

2

As you don´t have presented the Code you use for saving and loading your files (yet) here is my guess:

When using QTextEdit to save RichtText you have to save the text using toHtml() and not toPlainText().

Another way is to use the underlying QTextDocument and QTextDocumentWriter to save your file.

If you clarify your question about what you are using to edit and save the text I will edit this answer.

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46