Qt World Summit: Register Today!


QTWebEngineView doesn't load QRC html files (C++)



  • Hi,

    I'm working on a simple markdown viewer which uses the QtWebEngineView to render the HTML generated and it was working fine until recently, might be due to an update? (I'm currently compiling using 5.15.2)

    I don't know the exact problem but it seems that when I try to tell the QWebEngineView to load or setUrl to a HTML file from my QRC it doesn't show anything - it's just a white screen.

    If I try loading "https://google.com" everything loads correctly - and even other files from the QRC works (!?) I can load a .css file in from the QRC without issues and it will show the CSS in the window that I create, but not HTML.

    Does anyone have any ideas? I've even tried with the most basic index.html file that I could think of...

    This is my MainWindow constructor (at least the relevant parts):

    MainWindow::MainWindow(QString *index_file, QString *file, QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) {
      m_ui->setupUi(this);
    
      ...
    
      m_file = new QFile(get_file(*file));
    
      m_channel = new QWebChannel(this);
      m_channel->registerObject(QStringLiteral("content"), &m_content);
    
      m_page = new WebPage(this);
      m_page->setWebChannel(m_channel);
    
      m_ui->WebView->setPage(m_page);
      ....
    
      QUrl file_url(*index_file);
      m_ui->WebView->setUrl(file_url);
      ....
    }
    

    The whole code is available here: GitHub, but I can post relevant parts if need be.

    Thanks in advance!



  • @peterkmoss You have to set the resource schema, in the case of qresource you have to use qrc:

    "qrc:/index-light.html";
    "qrc:/index-dark.html";
    


  • I'm using a QUrl of "qrc:/index-light.html" and my QRC file looks like this:

    <!DOCTYPE RCC><RCC version="1.0">
        <qresource prefix="/">
            <file>help.md</file>
            <file>index-dark.html</file>
            <file>index-light.html</file>
            <file>colors.css</file>
            <file>light.css</file>
            <file>dark.css</file>
            <file>markdown.css</file>
            <file>3rdparty/marked.js</file>
            <file>3rdparty/prism.js</file>
            <file>3rdparty/prism.css</file>
        </qresource>
    </RCC>
    

    It also doesn't say that it can't find the file - so it clearly knows that there is a html-file, it just doesn't show it?



  • @peterkmoss According to your code no:

    // https://github.com/Peterkmoss/qmarkdown/blob/fix-not-loading-index-file/src/main.cpp#L26
    *index_file = ":/index-light.html";
    
    // https://github.com/Peterkmoss/qmarkdown/blob/fix-not-loading-index-file/src/main.cpp#L30
    *index_file = ":/index-dark.html";
    

    I recommend using qDebug() to see what is failing, for example in your case: `

    QUrl file_url(*index_file);
    qDebug() << url;
    m_ui->WebView->setUrl(file_url);
    

    Have you tried changing to : *index_file = "qrc:/index-light.html"; and *index_file = "qrc:/index-dark.html"; in main.cpp?



  • First off, thanks for your time.

    I've tried that yeah - you can see that I've made the default index_file "qrc:/index-light.html" in the load_args function. It's here in the code.

    When writing to qDebug() the string is correctly "qrc:/index-light.html", though nothing is shown in the webview... I've tried changing it to "qrc:/light.css" and then it correctly shows the css in the webview (the code in plaintext).