Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

QWebEngineView no longer render html files with no .html extension



  • Hi,
    Last week my company upgraded the Qt version to Qt5.15.1 and now I have several issues with my code.
    One of them is that the QWebEngineView::load no longer render the html code set in the file: the html code is displayed instead of its rendering.
    After 2 days of investigation I realized that if I add .html at the end of my file name it works!
    It was working with any file name with Qt5.9.
    Is it a regression or is it a new requirement?

    Here is a simple example that illustrate this issue:
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QDialog* dialog = new QDialog (nullptr);
    
    QVBoxLayout* vertLayout = new QVBoxLayout (dialog);
    QDialogButtonBox* buttonBox = new QDialogButtonBox (QDialogButtonBox::Close, dialog);
    
    QWebEngineSettings::globalSettings ()->setAttribute (QWebEngineSettings::AutoLoadImages, true);
    QWebEngineSettings::globalSettings ()->setAttribute (QWebEngineSettings::PluginsEnabled, true);
    QWebEngineSettings::globalSettings ()->setAttribute (QWebEngineSettings::JavascriptEnabled, true);
    
    QWebEngineView* web_view_ = new QWebEngineView (dialog);
    web_view_->setMinimumHeight (500);
    web_view_->setMinimumWidth (700);
    vertLayout->addWidget (web_view_);
    vertLayout->addWidget (buttonBox);
    dialog->connect (buttonBox, SIGNAL (rejected ()), dialog, SLOT (reject ()));
    dialog->resize (QSize (1200, 800));
    
    QString file_name = "C:/Users/XXX/AppData/Local/Temp/report.txt";  // if here I use report.html it works!!
    web_view_->load (QUrl::fromLocalFile (file_name));
    
    dialog->show();
    return a.exec();
    

    }

    and here is the contents of the report.txt file:

    <html>
    <body><h1>Training Set Construction Summary</h1>
    Survey: TEXAS3D@vm -pau-pns-2 <br>
    Domain: Time Migrated<br>
    <br>
    <b>Seismic Attributes : </b><br>
    -Amplitude<br>
    <br>
    Filter out constant value traces : yes<br>
    <br>
    <b>Extraction Geometry</b><br>
    -Top Surface : CHANNELROOF UPGRADE RandD TEXAS3D Picking Interpretation - Offset Above : 0 ms<br>
    <br>
    -Bottom Surface : CHANNELROOF UPGRADE RandD TEXAS3D Picking Interpretation - Offset Below : 20 ms<br>
    <br>
    </body>
    </html>


  • Lifetime Qt Champion



  • @Ghis64 said in QWebEngineView no longer render html files with no .html extension:

    One of them is that the QWebEngineView::load no longer render the html code set in the file:

    It was working with any file name with Qt4.9.

    How was that, given that Qt 4 did not have QWebEngine... anything? Must have been miraculous....



  • @jsulm said in QWebEngineView no longer render html files with no .html extension:

    @Ghis64 You can use https://doc.qt.io/qt-5/qwebengineview.html#setHtml instead of load()

    HI @jsulm. Nonetheless, this does seem to be a "gotcha"! Is it intentional? (Might help if the docs for load() said so?) Does this .htm vs .html behaviour apply to Linux, or is it a Windows-thing only?

    Whoops, I mis-read OP's situation, explained later on below.



  • @jsulm setHtml has a limit of 2 MB and it fails with some of our files that display plots with a lot of data.


  • Lifetime Qt Champion

    @JonB Sure, it could be documented (and should). I think it simply behaves like any web-broweser: if you enter an URL to a *.txt file the browser will not parse its content, but simply show the text even if it contains valid HTML.


  • Lifetime Qt Champion

    @Ghis64 Is there a reason why you name HTML files like text files?



  • @jsulm In fact in my application code I don't use the .txt extension (I have done like this only in my example). In the application we had this code :
    QTemporaryFile* tf = new QTemporaryFile (dialog);
    if (!tf->open ()) {
    return 0;
    }

    tf->write (html_text.toLatin1 ());
    tf->close ();
    QString file_name = tf->fileName ();
    web_view_->load (QUrl::fromLocalFile (file_name));
    

    I have replaced it by:

    QTemporaryFile* tf_html = new QTemporaryFile ("XXXXXX.html", dialog);
    if (!tf_html->open ()) {
        return 0;
    }
    
    tf_html->write (html_text.toLatin1 ());
    tf_html->close ();
    QString file_name_html = tf_html->fileName ();
    web_view_->load (QUrl::fromLocalFile (file_name_html));

  • Lifetime Qt Champion

    @Ghis64 said in QWebEngineView no longer render html files with no .html extension:

    I have replaced it by:

    Does it work now?



  • @jsulm Yes :) But I would have saved a lot of time if it was indicated in the documentation...



  • @jsulm said in QWebEngineView no longer render html files with no .html extension:

    @JonB Sure, it could be documented (and should). I think it simply behaves like any web-broweser: if you enter an URL to a *.txt file the browser will not parse its content, but simply show the text even if it contains valid HTML.

    Sorry, I seem to have quite mis-read what the OP said/meant! I had understood him to be saying he used to use .htm and now found he needs to use .html. Hence my question about Linux vs Windows. But I now see he says nothing of the kind...

    @Ghis64
    Now that I understand you had always used .txt. Then it's not a documentation/behaviour issue. Browsers should always open .txt files as text, only .htm or .html should be opened as HTML.


Log in to reply