1
votes

There are some limitations of QToolTip.showText/hideText as well as QWidget.setToolTip() which make it inconvenient for my needs. So I want to implement my own class that would behave similar as tooltips. I am using QLabel, set the windws flags and attributes and everything works fine except that it is not transparent for mouse events.

When creating a widget that should have this custom tooltip, I use the following code snippet:

toolTipWidget = QtGui.QLabel()
toolTipWidget.setFrameShape(QtGui.QFrame.StyledPanel)
toolTipWidget.setWindowFlags(QtCore.Qt.ToolTip)
toolTipWidget.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
toolTipWidget.hide()
self.toolTipWidget = toolTipWidget

in mouseMoveEvent of the widget I have the following:

def mouseMoveEvent(self, event):
    text = self.getDescription(event.pos())  # some method which returns a string
    if text:
        self.toolTipWidget.setText(text)
        self.toolTipWidget.move(event.globalPos() + QtCore.QPoint(5, 5))
        self.toolTipWidget.adjustSize()
        self.toolTipWidget.show()
    else:
        self.toolTipWidget.hide()

As you see in the code, the tool tip moves with the mouse. But sometimes it may happen that mouse cursor moves fast enough to enter the area of the tool tip and then the tool tip takes over the mouse events, which means that the mouseMoveEvent of the main widget is not called and the tool tip stops moving and gets stuck. This is strange because I set the attribute WA_TransparentForMouseEvents. Does anyone know how to solve this issue?

1

1 Answers

1
votes

I still have not found why WA_TransparentForMouseEvents is ignored in my custom tool tip. But I found a workaround that solves my issue. I installed event filter for the main widget that handles the mouse move events over my custom tool tip and calls the same code as the main widget's mouseMoveEvent handler - i.e. moves the tool tip widget away from the mouse cursor.