How to load multiple css files in QWebView

  • Dear All,
    I am able to load the single CSS file using the below code:

    view->settings()->setUserStyleSheetUrl( QUrl::fromLocalFile("D:/mycss/mycss1.css") );

    But i don't know how to do the same for multiple CSS files like css1.css, css2.css, css3.css
    Only the latest included css file is effective and rest css files are ignored.

    Kindly help!!


  • Do all these CSSes (css1.css, css2.css...) style the same elements/selectors? If yes, then it's how CSS works. Only the latest one will have an effect (will overlap all previous).

    Anyway, you can (either/or):

    1. Merge all your CSS into one file;
    2. Create a "general" one and use @import url("css1.css"); for every CSS you want in it;
    3. Add them from HTML with <link rel="stylesheet" type="text/css" href="path2css.css"/>? I don't know if it's possible within QWebView.

  • @veryqtperson Thanks for your reply.
    I tried the same thing. I created one common CSS file and added import for every CSS i need. I am able to load the CSS in browser but somehow its not reflecting in QWebView. Any idea how to debug further?

    Thanks in advance !!


  • Unfortunately, that's where my expertise ends, as I haven't done anything with QWebView yet :)

  • @veryqtperson No issues. Thanks for your time :)

  • Moderators

    a possibility would be to read all your CSS files and concatenate them into a string. Then pass the base64 encoded string to the webview.

    webview->settings()->setUserStyleSheetUrl( QUrl( QString("data:text/css;charset=utf-8;base64,") % QByteArray( CSS-string ).toBase64() ) );

  • @raven-worx Thanks !!

Log in to reply

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