问题
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?
回答1:
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);
I hope it helps.
回答2:
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
来源:https://stackoverflow.com/questions/28183738/transparent-background-in-qwebenginepage