9

We are trying to port some application from Qt 4 to Qt 5.4. The Qt 5.4 has a new web engine. We used to make the background of QWebView and QWebPage to be transparent:

view = new QWebView(this);
QPalette palette = view->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
view->page()->setPalette(palette);
view->setAttribute(Qt::WA_OpaquePaintEvent, false);

But this code doesn't work for QWebEngineView and QWebEnginePage. The point is that QWebEnginePage doesn't have such an API like setPalette.

Can anyone find a way to solve this?

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
TerryLu
  • 136
  • 1
  • 5

2 Answers2

12

As mentioned in https://bugreports.qt.io/browse/QTBUG-41960, this is now working by simply using this line:

webEngineView->page()->setBackgroundColor(Qt::transparent);

I've tried it in Qt 5.6 and it works well.

Update: In order to make this answer more helpful, let me show all relevant code.

In MainWindow, I have set this:

setAttribute(Qt::WA_TranslucentBackground);
setAutoFillBackground(true);
setWindowFlags(Qt::FramelessWindowHint);

For the webEngineView object, I have set these attributes:

webEngineView->setAttribute(Qt::WA_TranslucentBackground);
webEngineView->setStyleSheet("background:transparent");
webEnginePage = webEngineView->page();
// https://bugreports.qt.io/browse/QTBUG-41960
webEnginePage->setBackgroundColor(Qt::transparent);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jake W
  • 2,788
  • 34
  • 39
  • Unfortunately, this does not work at all. When I use this code in a webEngineView that is inside a transparent window (to display on top of another window), the background of a website that contains nothing but a div with text remains white. Tested with Qt 5.7. Using a different widget with a QGraphicsOpacityEffect on it works, so transparency does work... just not with a QWebEngineView, it seems. – TheSHEEEP Feb 28 '17 at 08:49
  • After fiddling around a bit more, it seems that the background of the PAGE does become invisible indeed. So I guess it does work. However, the background of the QWebEngineVIEW remains white so it isn't really helping much. – TheSHEEEP Feb 28 '17 at 08:58
  • In your HTML, did you use transparent background in your CSS? like this: body { background: transparent; } otherwise, I think by default the HTML would have a white background. – Jake W Feb 28 '17 at 13:40
  • Yes, of course. I did that, and as I said the color is actually applied. But there is always white behind it. And there is nothing behind the QWebEngineView on the Qt side that could be white, so it must be the view. A gave up on that by now and am doing the rendering of the browser site myself. – TheSHEEEP Feb 28 '17 at 13:47
  • 1
    In the MainWindow, I have this: setAttribute(Qt::WA_TranslucentBackground); setAutoFillBackground(true); setWindowFlags(Qt::FramelessWindowHint); For view, I have these: webEngineView->setAttribute(Qt::WA_TranslucentBackground); webEngineView->setStyleSheet("background:transparent"); webEnginePage = webEngineView->page(); // https://bugreports.qt.io/browse/QTBUG-41960 webEnginePage->setBackgroundColor(Qt::transparent); I hope it helps, I haven't tested against Qt5.7, but I think it should work in similar way. – Jake W Feb 28 '17 at 14:23
  • @TheSHEEEP I also updated the answer with better format for code. – Jake W Feb 28 '17 at 14:27
  • Very interesting. I had definitely tried out these exact settings, too, but it did not work. Now it did, though. The difference was that I set Qt::transparent AFTER the page was loaded, while you set it before loading the page. That seems to make all the difference (I can reproduce that behavior). Seemingly, Qt uses that setting to construct the page background, but won't touch it again if changed afterwards. – TheSHEEEP Mar 01 '17 at 06:54
  • To end this discussion: I reported this as a bug on Qt: https://bugreports.qt.io/browse/QTBUG-59216 – TheSHEEEP Mar 01 '17 at 07:09
0

No. A partial solution has been committed to upstream, but it covers only QtQuick and you can't have any elements on top:

https://bugreports.qt.io/browse/QTBUG-41960

Siana
  • 145
  • 4