Pregunta

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;
}
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top