Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent Background in QWebEnginePage

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?

like image 784
TerryLu Avatar asked Jan 28 '15 02:01

TerryLu


1 Answers

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);
like image 59
Jake W Avatar answered Oct 23 '22 04:10

Jake W