Important: Please read the Qt Code of Conduct -

How to import js and css into html file

  • Hello all,

    I'm loading an html code using QTWebEngine, but it does not load the external files in the html code.

    This is my index.qml file:

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtWebEngine 1.0
    ApplicationWindow {
    id: window
    visible: true
    WebEngineView {
    id: webEngineView
    anchors.fill: parent
    Component.onCompleted: {
    var html="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
    <html xmlns=''>
      <base href='/'>
      <title>Markup Converter Assistant</title>
      <meta name='viewport' content='width=device-width, initial-scale=1'>
      <meta http-equiv='X-UA-Compatible' content='IE=edge'>
      <meta property='og:image' content='conversation.svg' />
      <meta property='og:title' content='Conversation Chat Simple' />
      <meta property='og:description' content='Sample application that shows how to use the Watson Assistant API to identify user intents' />
      <link rel='shortcut icon' href='favicon.png' type='image/png'>
      <link rel='stylesheet' href='css/app.css'>
      <!-- <div id='view-change-button' class='button' onclick='PayloadPanel.togglePanel(event, this)'>
        <img class='option full' src='../img/Chat Button.png'>
        <img class='option not-full' src='../img/Code Button.png'>
      </div> -->
      <div id='contentParent' class='responsive-columns-wrapper'>
        <div id='chat-column-holder' class='responsive-column content-column'>
          <div class='chat-column'>
            <div id='scrollingChat'></div>
            <p class='user-typing' id='user-typing-field'></p>
            <label for='textInput' class='inputOutline'>
              <input id='textInput' class='input responsive-column' placeholder='Type something' type='text' onkeydown='ConversationPanel.inputKeyDown(event, this)'
        <div id='payload-column' class='fixed-column content-column'>
          <div id='payload-initial-message'>
            Type something to see the output
          <div id='payload-request' class='payload'></div>
          <div id='payload-response' class='payload'></div>
      <script src='js/common.js'></script>
      <script src='js/api.js'></script>
      <script src='js/conversation.js'></script>
      <script src='js/uiActionFunctions.js'></script>
      <script src='js/payload.js'></script>
      <script src='js/global.js'></script>
      <script type='text/javascript' src=''></script>

    The html is rendering in a pop up window as I want but the scripts and stylesheet is not imported. Please what's the right way to rewrite this qml scripts so I can import and load the scripts and stylesheets?

  • Lifetime Qt Champion

    @sylvia said in How to import js and css into html file:


    This is a relative path. At runtime it is relative to the directory where your executable is. Does that directory contain css and js subdirectories?

  • @jsulm said in How to import js and css into html file:

    At runtime it is relative to the directory where your executable is

    You sure about this? I don't do any QML so I don't know. It you load a QWebEngine from a URL I would expect it to be relative to the URL's directory. Here I don't know, it could be relative to where the QML came from, or even the current directory? I respect your greater knowledge, is the QML compiled into the executable as a "resource" or something and that's why it is relative to executable directory?

  • Lifetime Qt Champion

    @JonB said in How to import js and css into html file:

    I respect your greater knowledge

    Actually I don't have greater knowledge :-)
    My QML expertice is very limited, it just looks like something relative and usually it is relative to the directory where the executable is, not sure in case of QML though.

  • @jsulm
    I remain to be shot down, and at least you sound like you have used QML! But here it's a bit more complicated, if the user says this is being loaded by QWebEngine from QML. When I used QWebEngine from a QWidgets application, and it was from in-memory HTML not from a file, I think paths were relative to... In truth I don't know, looks like in my code I pre-processed HTML literal to load <link rel="stylesheet" href="..."> style files into the HTML literal in the first place.

    Suggest the OP try relative to executable directory, but be prepared to try elsewhere if that turns out not to load the file.

Log in to reply