QTextEdit with html and image export
Solved
General and Desktop
-
I am using QTextEdit with html for loading and displaying an html files including images. Recently added "Save as ..." fails on saving the file in a different location because the images having a relative path.
Do I have to search and change the html with own code for detecting and adjusting the path to images or is there some better solution already in Qt?
My searches were not successful.
-
-
I have found a solution suiting me.
QRegularExpression rexp ( "<img src=\\\"([^\\\"]{1,})([^/]{0,})/>" ); QRegularExpressionMatch match; int pos = content.indexOf ( rexp, pos + 1 ) ; if ( pos > -1 ) { match = rexp.match ( content, pos ); if ( match.isValid() ) { QString imFile = match.captured ( 1 ); } }
imFile will hold the file name stored.
Or as alternative for the deprecated QRegExp. The expression itself works for both.
QRegExp rexp ( "<img src=\\\"([^\\\"]{1,})([^/]{0,})/>" ); int pos = content.indexOf ( rexp ); if ( pos >= 0) { QString imFile = rexp.cap ( 1 ); }
Note: The indexOf with QRegularExpression may also handle the QRegularExpressionMatch since version 5.5. This would make it more compact.