hovering over child widgets, Qt::WA_NoMousePropagation not working
-
Hi guys,
I have recently posted a qt question on SO, but I did not get an answer there. It is quite important to me and i have already spent a lot of time with it, therefore i gonna try it here too. Following the question:I have a parent widget containing some child widgets. The problem I am facing is that when I hover over the child widgets, the parent widget is getting hovered too.
So I have gone through Qt's doc and it says in http://doc.qt.io/archives/qt-4.8/qhoverevent.html that Qt::WA_NoMousePropagation is exactly what i need. But it's simply not working.
Here is a basic example
#include <QLabel> #include <QHBoxLayout> class Label : public QLabel { Q_OBJECT public: Label(QWidget *parent = Q_NULLPTR) : QLabel("parent",parent), childLabel(new QLabel("child")) { this->setWindowState(Qt::WindowMaximized); childLabel->setAttribute(Qt::WA_NoMousePropagation); //<-not working childLabel->setObjectName("childLabel"); auto hLayout = new QHBoxLayout; hLayout->addWidget(childLabel,0,Qt::AlignRight); hLayout->setContentsMargins(0,0,0,0); setLayout(hLayout); setStyleSheet(R"( QLabel:hover{background-color: blue;} #childLabel:hover{ background-color: yellow;} )"); } ~Label() = default; private: QLabel *childLabel; };
Check it out and you will see that the parent is still hovered, when you hover the child... Am I missing here something or is Qt::WA_NoMousePropagation not working. Is there any workaround?
Thank you in advance
Best Regards
-
Hi and welcome to the forums.
I will try in a moment, but since you already have a subclass, did you also try
overriding void QWidget::enterEvent(QEvent * event)
and set event as handled ? -
Wait a minute
class Label : public QLabel is parent
childLabel(new QLabel("child")) is the QLabel you want to hover ?But you set the stylesheet to the parent (and it affects the QLabel child too)
setStyleSheet(R"(
QLabel:hover{background-color: blue;}
#childLabel:hover{ background-color: yellow;}
)");dont you mean
childLabel->setStyleSheet(R"(
QLabel:hover{background-color: blue;}
#childLabel:hover{ background-color: yellow;}
)"); -
Hi,
Thank you for your fast reply !So what i want is to be able to hover both, but seperately, when mouse is over the child(childLabel) it should only! hover the child else it should hover the widget(the Label). (e.g. like in the doc but HoverMove should be like MouseMove)
More or less i do not want the childLabel's HoverMove to be propagated to its parentI have considered reimplementing the enterEvent and somehow go on with comparing with HoverMove, but it is written in the doc that Qt::WA_NoMousePropagation attribute has to be set to get the desired effect. But I cant see any effect setting this attribute.
-
Hi
Yes you are right. it should only hover one.
Do you use class Label : public QLabel as the window ? ( and no mainwindow)
(going to test it) -
No, thats actually a bigger project, this was just a small illustrative example.
I have ofc. a mainwindow and so on...
I noticed this problem when i put a ComboBox(as selector) inside a StackedWidget. I am subclassing the widgets directly in order to inherit all the public members.
I did something like this ...class StackedWidget : public QStackedWidget { StackedWidget(...) {analogous to above example} QComboBox pageComboBox; }
-
Hi
I agree - its a bit odd.
QLabel might have some special flag.
Did you try with Qwidgets ? -
I have tried it with QComboBox/QStackedWidget because thats actually where i need it.
-
@OZaric
Just tried with QWidgets and mainwindow and indeed the mouse
bleed through. -
Anyway Thank You for your time !
-
@OZaric
It seems that Qt::WA_NoMousePropagation have no effect
Im using Qt 5.10
What version are you testing with ? -
I am working with Qt 5.9.3. I have here a few (including static builds), tested on all without success.
-
-
Hi,
Can you share your test sample ?
-
-
Samuel Gaist, the qt legend :D
I am reading you already for years in this forum!Check it out, it seems like setting attr Qt::WA_NoMousePropagation has no effect at all.
-
It is used in QComboBox.
Did you already saw the QHoverEvent description especially the part about propagation ?
-
-
Hi
The sample sets Qt::WA_NoMousePropagation on "CHILD"
in mainwindows ctor but it made no difference.The CHILD label seems to be aligned with Parents client rect and in theory
cursor should be able to enter the CHILD directly but i suspect the truth is
that entering a widget always enters the parent first regardless of the visual
representation. -
Sorry, I misread the link you used.
As for QComboBox, yes I am. See here.
10/21