使用 QPlainTextEdit 保存为 HTML
我正在使用 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QPlainTextEdit 有一个名为 document() 的方法,它返回一个 QTextDocument。那有一个可以使用的 toHtml 函数。 HTH。
The QPlainTextEdit has a method called document() that returns a QTextDocument. That has a toHtml function which can be used. HTH.