Qt::WA_TranslucentBackground not work for QWidget
-
Enviroment: Qt5.15.2 and windows10
Problem: I put a QWidget which has set a QGridLayout on top of a QWebView by QStackedWidget, and set attribute of QWidget toQt::WA_TranslucentBackground
, I can see the QWebView under the QLabel but I can not operate it.
ps: Even without set QLayout, QWidget can not propagate the mouse event.QtGuiApplication1::QtGuiApplication1(QWidget *parent) : QMainWindow(parent) { QWebEngineView* pWebWidget = new QWebEngineView(); pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development")); QWidget* pWidget = new QWidget(); QGridLayout* pGridLayout = new QGridLayout(); QPushButton* pButton = new QPushButton("Label"); pGridLayout->addWidget(pButton, 0, 0, Qt::AlignLeft); pWidget->setLayout(pGridLayout); pWidget->setWindowFlags(Qt::FramelessWindowHint); pWidget->setAttribute(Qt::WA_TranslucentBackground); QStackedLayout* pStackedLayout = new QStackedLayout(); pStackedLayout->addWidget(pWebWidget); pStackedLayout->addWidget(pWidget); pStackedLayout->setStackingMode(QStackedLayout::StackAll); QWidget* pCentralWidget = new QWidget(); pCentralWidget->setLayout(pStackedLayout); this->setCentralWidget(pCentralWidget); }
Picture:
-
Hi,
Are you looking for the
Qt::WindowTransparentForInput
Window flag ? -
How are you applying it ?
-
@SGaist Hey.
I want a QLyout which has many buttons on the top of the QWebEngineview.
Both of them can auto resize themselves, and the rest transparency area of the QLayout can propagate event like mouse click or wheel event.
But seems like QLayout must set to a Qwidget so that it can be resized.
I can implement this idea by eventfilter so that i can pass through the event, but any other ways?
Thank you. -
Hence my question:
@SGaist said in Qt::WA_TranslucentBackground not work for QWidget:
How are you applying it ?
I haven't used it in a long time so what happens if you apply the flag only on the widget that you make transparent and that will likely contain your buttons.
-
@SGaist said in Qt::WA_TranslucentBackground not work for QWidget:
How are you applying it
Sorry for my off-topic reply.
If you mean the flag ofQt::WindowTransparentForInput
, I apply it bypWidget->setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint); pWidget->setAttribute(Qt::WA_TranslucentBackground);
Nothing changed just like my original code, buttons can work but you can't manipulate the web.
-
Just to check, what happens if you do not use QStackedWidget but manually position pWidget on top of your web view ?
-
@SGaist ok, I have tried this before.
m_pWebWidget = new QWebEngineView(this); m_pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development")); m_pWebWidget->setFixedSize(200, 200); m_pWebWidget->show(); m_pWidget = new QWidget(this); m_pWidget->setFixedSize(200, 200); m_pWidget->setWindowFlags(Qt::WindowTransparentForInput | Qt::FramelessWindowHint); m_pWidget->setAttribute(Qt::WA_TranslucentBackground); m_pWidget->show();
The web view is not operable either.
-
Qt::WA_TranslucentBackground
just change the painting of background, doesn't ignore the mouse events.
Qt::WA_TransparentForMouseEvents
is the attribute for that, but it will also make its child widget (the button) not clickable.
(PS. I think it's not necessary to setQt::FramelessWindowHint
to a non-window widget.)
Currently the easier way I can think of is, just create a button as the web view's child widget and control its geometry manually without any layout.
By this way you don't need to set any flag / attribute to have a clickable button and a operable web view.QWebEngineView* pWebWidget = new QWebEngineView(); pWebWidget->load(QUrl("https://forum.qt.io/category/4/qt-development")); QPushButton* pButton = new QPushButton("Label", pWebWidget); pButton->move(0, 100); this->setCentralWidget(pWebWidget);
If you need to adjust the geometry of the button when window size changes, do it in
resizeEvent
. -
@Bonnie Yeah, I think Qt may not have exact flag/attribute I need.
And what you say it's a useful way to solve my quesiton if I only need to manage a few objects.
Without QLayout it will become a strenuous work in further.
I think below code is a feasible way to solve similar question:bool QtGuiApplication1::eventFilter(QObject *watched, QEvent *event) { if (watched == m_pWidget && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::Wheel)) { if (m_pWebWidget->focusProxy()) { QCoreApplication::sendEvent(m_pWebWidget->focusProxy(), event); } } return false; }
Thank you for your advice and i will use it.