How to serialize and deserialize rich text in QTextEdit?

Let's 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 these 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 it:

QFile file("myFileName");
file.open(QIODevice::WriteOnly))
QDataStream out(&file);
out << AAA_object;

to serialize an AAA object to a file.

Now the question. How should I define the QDataStream statements for the BBB class to serialize the BBB object (int, QString and QTextEdit to reach the text content ) when I call out <AAA_object;

+3
source share
3 answers

. QVector. HTML. QVector. :

for(int i = 0; i < vectorOfImages.size(); 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("<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);

! :)))!

+1

QTextEdit - , , (QTextEdit:: toHtml()) . (QTextEdit:: setHtml()).

, , , BBB ( html QString), QTextEdit.

+2

:

( roop) QTextEdit , (QTextDocument). QTextEdit QTextEdit:: document().

QTextDocument* pTextDoc = m_textEdit->document();

html , QByteArray:

QString MyText = pTextDoc->toHtml();
QByteArray TextAsByteArray = MyText.toUtf8();

QByteArray, , < < " > .

QByteArray QString (. QString:: fromUtf8()) QTextDocument:: setHtml(), QTextEdit.

UPDATE

jpalecek, . QString, HTML, QString:: operator < <() QString:: operator β†’ () QByteArray.

0
source

Source: https://habr.com/ru/post/1779470/


All Articles