c++ - 使用 QPlainTextEdit 保存到 HTML

标签 c++ qt4 save text-editor qplaintextedit

我正在使用 Qt C++ 框架编写一个文本编辑器。我使用 QPlainTextEdit 作为中央小部件,用户可以在其中写出他的文档。文本可以是粗体、斜体和彩色。

我在编写保存方法时遇到了问题。我想保存格式,但我发现的只是 toPlainText() 函数,这显然意味着所有格式都丢失了。如何保存格式?

我附上了保存功能的代码,以防我的问题不清楚:

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;
}

最佳答案

QPlainTextEdit 有一个名为 document() 的方法,它返回一个 QTextDocument。它有一个可以使用的 toHtml 函数。 HTH.

关于c++ - 使用 QPlainTextEdit 保存到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8509099/

相关文章:

c++ - 在数据库中创建一个新表,并将现有表的描述作为输入

Node.js 解释 REPL 中的错误?

ios - 从 UITextView 保存可编辑的长文本

c++ - 链接 zmq c++ header 时出错

c++ - 如何修复这种典型的异常不安全代码?

c++ - 使用 std::copy 从随机数分布中复制

Qt 颜色选择器小部件?

c++ - 将值复制到 QStringList 和从 QStringList 复制到 QString Array[]

android - 如何保存最后一个 fragment 的数据

c++ - 如何让递归函数输出数字从f(0)开始,让f(n)向上计数