Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add css stylesheet to a QWebEngineView?

How to add css stylesheet to a QWebEngineView?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 503 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylvy
    wrote on last edited by
    #1

    I'm trying to follow this Qt example for adding CSS stylesheet to a QWebEngineView, however, I'm getting this error:

    use of undefined type 'QWebEngineScriptCollection'
    

    at the line ui.webEngineView->page()->scripts().insert(script);

    What I'm missing?
    Testing using qt 6.4, Visual Studio 2022, the project can be reproduced by simply adding a QWebEngineView to the ui, and

    #include "stdafx.h"
    #include "MainWindow.h"
    #include <QWebEngineScript>
    
    void MainWindow::insertStyleSheet(const QString& name, const QString& source, bool immediately)
    {
        QWebEngineScript script;
        QString s = QString::fromLatin1("(function() {"\
            "    css = document.createElement('style');"\
            "    css.type = 'text/css';"\
            "    css.id = '%1';"\
            "    document.head.appendChild(css);"\
            "    css.innerText = '%2';"\
            "})()").arg(name).arg(source.simplified());
    
        if (immediately)
            ui.webEngineView->page()->runJavaScript(s, QWebEngineScript::ApplicationWorld);
    
        script.setName(name);
        script.setSourceCode(s);
        script.setInjectionPoint(QWebEngineScript::DocumentReady);
        script.setRunsOnSubFrames(true);
        script.setWorldId(QWebEngineScript::ApplicationWorld);
        ui.webEngineView->page()->scripts().insert(script);
    }
    
    
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
        ui.setupUi(this);
    }
    
    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      Ylvy
      wrote on last edited by
      #2

      Fixed it. The correct include is:

      #include <QtWebEngineCore/qwebenginescriptcollection.h>
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved