QTextEdit with html and image export


  • Moderators

    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.


  • Moderators

    Hi Koahnig,

    Maybe this topic can help you in the right direction?

    Hope it helps,

    Eddy


  • Moderators

    @Eddy

    Thanks for reply.

    Not exactly. I am trying to avoid the situation that there is already everything handled in Qt directly and I am trying to find and substitute the string without need.


  • Moderators

    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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.