简体   繁体   中英

Change style on Hover state of parent QWidget in Qt

I have a composite control in Qt that is a QWidget with a QHBoxLayout containing 3 sub controls (QWidget > QHBoxLayout > 3 QLabels). On one of those controls, which is just a QLabel displaying an icon, I want it to change it's icon when hovering over the entire control. I can get it to work when actually hovering over that icon

QLabel#stateIcon {
    image: url(:/icons/stateNormal);
}

QLabel#stateIcon:hover {
    image: url(:/icons/stateHover);
}

However, I would like it to show the hover icon when the mouse is anywhere over the whole control.

I tried something like the following:

QLabel#stateIcon::parent:hover {
    image: url(:/icons/stateHover);
}

and even

QLabel#stateIcon::parent::parent:hover {
    image: url(:/icons/stateHover);
}

Hoping that it would activate on the hover-state of the parent but it does nothing.

Is there a way to do this purely in stylesheets?

If not, is there a way to propagate the parent hover state to it's children in code?

Maybe with an event handler dealing with the QEvent::HoverMove or QEvent::HoverEnter and QEvent::HoverLeave where you can set the attribute Qt::WA_Hover to your label.

I didn't try it but it should work as HoverEnter and HoverLeave causes update() function to be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM