Question

Say there is a text and images in a QTextEdit. And say you extract HTML code of the QTextEdit. Now how it is posible to find the places in the HTML code where the images are instantiated () and replace with another images that I have as a QImage objects retaining the position and the size of the former image.

In other words it is necessary to exchange pictures while retaining the position and the size of the first picture, when the second picture is stored in the program as a QImage.

Please consider that the first image may not exist at all. The HTML code that hase may be set to the QTextEdit artificially regardless there if is an image like file://path/to/image/image_name.png or not.

Was it helpful?

Solution

//Add images as resources
for(int i = 0; i < vectorOfImages.size(); i++ )
{
    QUrl url(QString("image_%1").arg(i));
    textEdit->document()->addResource(QTextDocument::ImageResource, url,  vectorOfImages.at(i));
}

//Process the htmlCode that is in QTextEdit.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top