3

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

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Narek
  • 38,779
  • 79
  • 233
  • 389

3 Answers3

2

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.

roop
  • 1,291
  • 8
  • 10
1

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 :)))!

Narek
  • 38,779
  • 79
  • 233
  • 389
0

Here is what I would do :

First (as roop said), you shouldn't store the QTextEdit widget itself, but the underlying text document (QTextDocument). You can get it from the QTextEdit widget with QTextEdit::document().

QTextDocument* pTextDoc = m_textEdit->document();

Then, I would get the html string from this document and from this string, get a QByteArray :

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

Once you have a QByteArray object containing your document, you can use the << and >> operators.

For reading back the QByteArray, store it into a QString(see QString::fromUtf8()), and use QTextDocument::setHtml() to display the content into the QTextEdit widget.

UPDATE

Following jpalecek comment, I'm overcomplicating the solution. Once you have a QString containing your text document as HTML, you can use QString::operator<<() and QString::operator>>() without using a QByteArray.

Jérôme
  • 26,567
  • 29
  • 98
  • 120
  • Why not store QString directly in the QDataStream, if it has operator< – jpalecek Dec 10 '10 at 09:54
  • @jpalecek : you're right, I'm overcomplicating ! I'll update my answer. – Jérôme Dec 10 '10 at 10:01
  • All this I read in forums too. But I worry if I will store images that are in the qtextedit. In general, the problem here is to load and reload images too. – Narek Dec 10 '10 at 10:56
  • @Narek : Have you tried this solution with images ? Worrying is not enough, you have to test, and check what is working and what is not ! Then, depending of the specific problem you are facing, ask for a more specific question. – Jérôme Dec 10 '10 at 12:07
  • Ok I see you are sure that this should work. I will try. But it is obvious that there is something to worry. Because in the HTML code there is no image file. There is only the instantiation of that file. So I wonder if I am not going to lose the picture, while retriving only the HTML code. – Narek Dec 10 '10 at 18:20
  • @narek, i'm not sure it will work but I guess it's a step towards a solution so yes it's worth trying. I understand your worries about images, it sounds fair you might have to implement something more, but you won't know exactly what until you go a bit further with experimentation. Let us know if you have troubles we'll be happy trying to help! – Jérôme Dec 10 '10 at 21:35
  • I have tried! It does not work. It takes the file location like "" and stores the HTML code. There is no image saved with the HTML in a file as a whole. – Narek Dec 10 '10 at 22:20
  • May be I should use a vector to serialize also the images for the class BBB? And also process the HTML code to change file paths? Can I do this? I am not sure that this is posible because when I deserialize the QImages, how I will instantiate them in the HTML again? – Narek Dec 10 '10 at 22:28
  • There is maybe another way : write your QtextDocument into a .odt file (with `QTextDocumentWriter`). The problem: Qt does not support odf reading yet. You'll have to use a third party library. Have a look at this : http://stackoverflow.com/questions/3747180/populate-a-qtextdocument-from-a-odt-file – Jérôme Dec 11 '10 at 06:19