如何序列化和反序列化富文本在QTextEdit? [英] How to serialize and deserialize rich text in QTextEdit?
问题描述
我承诺+10 upvote到这个问题的答案只是从我身边!更重要的是,如果有办法,我会将所有的评分给正确答案的人,因为我需要实现这一点。
说我有这样的结构:
class AAA
{
BBB bb_member;
double dbl_member;
....................
}
class BBB
{
int int_member ;
QString QStr_member;
.................
QTextEdit m_textEdit;
}
对于AAA,我定义这个运算符:
QDataStream& operator {
out< aa.bb_member
<< aa.dbl_member;
return out;
}
QDataStream& operator>>(QDataStream& in,AAA& aa)
{
BBB bb_memb;
double dbk_memb;
在>> bb_memb
>> dbk_memb;
aa = AAA(bb_memb,dbk_memb);
return in;
}
然后我称之为:
QFile文件(myFileName);
file.open(QIODevice :: WriteOnly))
QDataStream out(& file);
out<< AAA_object;
以便将AAA对象序列化为文件。
现在的问题。如何为BBB类定义QDataStream操作符,以便序列化BBB对象(int,QString和 QTextEdit到达文本内容),同时调用 out< AAA_object;
我已完成此任务。我已经保存在QVector中的图像。序列化向量和HTML代码。然后反序列化的代码和QVector。使用此函数添加所有图片作为资源:
for(int i = 0; i {
QUrl url(QString(image_%1)。arg(i));
textEdit-> document() - > addResource(QTextDocument :: ImageResource,url,vectorOfImages.at(i));
}
然后执行以下
int count = 0;
int pos = 0;
QRegExp rx( while((pos = rx.indexIn(htmlCode,pos))!= -1 )
{
QString strToReplace(QString(< img src = \image_%1\/>\").arg(count));
htmlCode.replace pos,rx.matchedLength(),strToReplace);
pos + = rx.matchedLength();
count ++;
}
textEdit-> setText(htmlCode );
工作得很好!
I promise +10 upvotes to the answer of this question just from my side! Even more, if there is a way, I will give all my rating to the person who will answer with a correct answer, because I need to implement this very much.
Say I have a structure like this:
class AAA
{
BBB bb_member;
double dbl_member;
....................
}
class BBB
{
int int_member;
QString QStr_member;
.................
QTextEdit m_textEdit;
}
And for AAA I define this operators:
QDataStream &operator<<(QDataStream &out, const AAA &aa)
{
out << aa.bb_member
<< aa.dbl_member;
return out;
}
QDataStream &operator>>(QDataStream &in, AAA &aa)
{
BBB bb_memb;
double dbk_memb;
in >> bb_memb
>> dbk_memb;
aa = AAA(bb_memb, dbk_memb);
return in;
}
Then I call this:
QFile file("myFileName");
file.open(QIODevice::WriteOnly))
QDataStream out(&file);
out << AAA_object;
in order to serialize AAA object to a file.
Now the question. How I should define QDataStream operators for BBB class in order to serialize BBB object (int, QString and QTextEdit reach text content), while calling out << AAA_object; ???
I have already compleated this task. I have saved the images in a QVector. Serialized the vector and the HTML code. Then deserialized the code and the QVector. Added all the images as a resource with this function:
for(int i = 0; i < vectorOfImages.size(); i++ )
{
QUrl url(QString("image_%1").arg(i));
textEdit->document()->addResource(QTextDocument::ImageResource, url, vectorOfImages.at(i));
}
Then Does the following
int count = 0;
int pos = 0;
QRegExp rx("<img src=\".+/>");
while ((pos = rx.indexIn(htmlCode, pos)) != -1)
{
QString strToReplace(QString("<img src=\"image_%1\" />").arg(count));
htmlCode.replace(pos, rx.matchedLength(), strToReplace);
pos += rx.matchedLength();
count++;
}
textEdit->setText(htmlCode);
Works fine! And I will have my former rating :)))!
这篇关于如何序列化和反序列化富文本在QTextEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!