Search code examples
c++qttemporary-files

Howto open html page in default browser, when html code is stored in QString


My Qt application should open a html page (with the default browser eg. IE). This html code is stored in a QString.

What would be the best way to open this "file", of which I only have the content?

Is QTemporaryFile the answer to this? Or could this be done easier?

QString content = "<html>...</html>";
?
QDesktopServices::openUrl(QUrl("..."));

Solution

  • The QTemporaryFile approach is by far the easiest to accomplish your task.

    I don't see any other way other then doing some "vodoo" with ActiveQt, if that works at all.

    Best regards.

    EDIT: An example

    QString htmlData; // your HTML data here
    
    // The six Xs are actually required.
    QTemporaryFile tmpFile( QLatin1String( "thefileXXXXXX.html" ) );
    tmpFile.open();
    QTextStream out( &tmpFile )
    out << htmlData;
    tmpFile.close();
    QDesktopServives::openUrl( QUrl::fromLocalFile( tmpFile.fileName() ) );