3

I am writing a Text Editor using the Qt C++ framework. I'm using a QPlainTextEdit as the central widget where the user writes out his document. The text can be bolded, italicised and coloured.

I'm having a problem when it comes to writing the save method. I want to save the formatting, but all I've found is the toPlainText() function which obviously means that all the formatting is lost. How can I save the formatting?

I've attached the code for my save function, just in case my question isn't clear:

bool TextEditor::saveDocument(QString filePath)
{
    qDebug()<<"Saving File at"<<filePath<<endl;
    QFile document(filePath);
    if(!document.open(QFile::WriteOnly | QFile::Text))
    {
        qDebug()<<"An Error occur while opening "<<document.fileName()<<endl;
        return false;
    }
    QTextStream writer(&document);

    writer << ui->Editor->toPlainText();
    writer.flush();
    document.close();
    qDebug()<<"Document saved successfully.";

    if(this->document == NULL)
        this->setDocument(&document);

    return true;
}
W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • 1
    The QPlainTextEdit has a method called document() that returns a QTextDocument. That has a toHtml function which can be used. HTH. – shekhar Dec 14 '11 at 17:48
  • 1
    @user1087135 If you're pretty sure you know what the solution is, post it as an answer instead of a comment. Besides being a way to get points for an accepted answer, it takes the question out of the "unanswered" list...and allows the original poster to accept it to "close" the issue. – HostileFork says dont trust SE Dec 14 '11 at 18:14
  • In cases where a comment leads to a solution, the OP is encouraged to write an answer that includes that solution and accept it. But it does save work for the OP if the answer is posted as an answer :) – Arnold Spence Dec 14 '11 at 21:51
  • Sorry, am a novice at stackoverflow. Anyways posted it as an answer. – shekhar Dec 15 '11 at 05:38

1 Answers1

3

The QPlainTextEdit has a method called document() that returns a QTextDocument. That has a toHtml function which can be used. HTH.

shekhar
  • 1,372
  • 2
  • 16
  • 23