1
votes

I need a function that takes two Qt widgets and "links" them such that no matter which one the mouse is hovering over, they both get highlighted with their (standard) hover highlighting (if they have such).

This needs to work with whatever kinds of widgets the caller passes to this function; this function can't require callers to create widget subclasses just to make this work.

Here are some images of what I'm referring to:

enter image description here

Normally, when you hover the cursor over a widget, it gets highlighted as shown in Images 1 and 2. I need an example of how to set things up so that regardless of which widget the mouse is hovering over, they BOTH get highlighted with their standard hover highlighting, as shown in Image 3. Ideally, this function would work generically, only operating on its arguments as QWidgets, regardless of what kind of widgets they actually are (because hover highlighting is a generic Qt behavior, after all).

def link_highlighting(widgetA, widgetB):

   ... what should go here? ...

This feels like it would involve some pretty dark event hackery, but my event hacking fu is not strong.

Ideas? Suggestions?

1
I'm not sure what sort of example to give since I am asking for an example of how to do this. Do you mean you want an image of what standard Qt behavior looks like when widgets are highlighted during mouse hover?John Cooper
Exactly, I want a visual example of what you want since what you say is very generic that can be interpreted in many ways. In addition to requesting that the functionality be applied to generic widget and not saying that they are QPushButton, QLineEdit, etc. For the above, I might think you have an XY problem, what is your main objective?eyllanesc
Okay, I've updated the original message with an image and some more explanatory text.John Cooper

1 Answers

0
votes

I did a bit of research and I see two ways this can be done right now in a generic way.

The first part is detecting the hover action. This should be easy. Just subscribe to the enterEvent and leaveEnvent of both widgets. As long as they are of type QWidget this will work. With that you will be notified when the mouse enter or leaves a widget.

Then the highlighting part. Here you have two options:

  1. You can fake a mouse event on the second widget, basically emulating a second mouse enterEvent. This can be done with qApp.sendEvent(widget, event).

  2. You can change the palette of the second widget. Basically to set the normal palette to the highlighted palette. Here you would use functions like:

    palette = widget.getPalette()
    palette.setColor(QPalette.Base, widget.palette().color(QPalette.Highlight))
    widget.setPalette(palette)
    

    This obviously needs some experimenting. And for now this only works for widget styles that work with paletters. For the ones that work with CSS one would need to basically do the same. Find the bit that applies to highlight and copy it over to the normal state.

    Depending on how general your case is, this could be really easy or very hard.

Let me know how this goes.