Qt3ToQt4 How to convert custom tool tip to an event filter

From Qt Wiki
Revision as of 17:42, 12 March 2015 by AutoSpider (talk | contribs) (Decode HTML entity names)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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));