Qt3ToQt4 How to convert custom tool tip to an event filter

From Qt Wiki
Jump to: navigation, search

How to convert Qt 3 custom tool tip to Qt 4 event filter

QToolTip no longer supports inheritance. To customize tooltip behavior on widgets, intercept QHelpEvents in the widget's event() function. Here is how to use an event filter rather than a class formerly deriving from QToolTip.

Derive from QObject instead QToolTip

Change class MyToolTipper : public QToolTip to class MyToolTipper : public QObject

Declare the eventFilter rather than maybeTip.

Change maybeTip

void maybeTip( const QPoint &pos );

to protected:

bool eventFilter( QObject *obj, QEvent *event );

Convert the maybeTip() implementation to eventFilter()

bool MyToolTipper::eventFilter( QObject *obj, QEvent *event ) {

if (event->type() == QEvent::ToolTip) { // Only process tool tip events
QHelpEvent helpEvent = static_cast<QHelpEvent>(event); // Tool tip events come as the type QHelpEvent
QPoint pos = helpEvent->pos(); // Get pos from event (instead of what was passed to maybeTip)

// Code from maybeTip function. Modified for Qt 4.

// Rather than tip() use QToolTip::showText() and QToolTip::hideText()
// For example, convert
// tip(r, text);
// to
// QToolTip::showText(helpEvent->globalPos(), text, widget, r);

return true; // Return true to filter event

return false; // Return false to allow other event processing


Install event filter where custom tool tip class was used.

Change creation of custom tool tip

_toolTipper = new MyToolTipper(viewport(), this);

to installing the event filter.

viewport()->installEventFilter(new MyToolTipper(viewport(), this));