Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

c++ - How to serialize and deserialize rich text in QTextEdit?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

QTextEdit is a widget, and it doesnt make much sense to write a widget to a file, but we can write the content of the widget (QTextEdit::toHtml()) to the file. When reading from file, we can create a new widget object and initialize it with the contents of the file (QTextEdit::setHtml()).

I must add that it would probably be a better design to store just the richtext data in BBB (as a html QString) as opposed to the QTextEdit itself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...