Qt3ToQt4 How to convert custom tool tip to an event filter: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=How to convert Qt 3 custom tool tip to Qt 4 event filter=
[[Category:HowTo]]


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.
= How to convert Qt 3 custom tool tip to Qt 4 event filter =


==Derive from QObject instead QToolTip==
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.


Change<br /> to<br />
== Derive from QObject instead QToolTip ==


==Declare the eventFilter rather than maybeTip.==
Change<br /><code><br />class MyToolTipper : public QToolTip<br /></code><br />to<br /><code><br />class MyToolTipper : public QObject<br /></code>


Change maybeTip<br /> to<br />
== Declare the eventFilter rather than maybeTip. ==


==Convert the maybeTip() implementation to eventFilter()==
Change maybeTip<br /><code><br /> void maybeTip( const QPoint &amp;pos );<br /></code><br />to<br /><code><br />protected:<br /> bool eventFilter( QObject *obj, QEvent *event );<br /></code>


==Install event filter where custom tool tip class was used.==
== Convert the maybeTip() implementation to eventFilter() ==


Change creation of custom tool tip<br /> to installing the event filter.<br />
<code><br />bool MyToolTipper::eventFilter( QObject *obj, QEvent *event )<br />{<br /> if (event-&gt;type() == QEvent::ToolTip) { // Only process tool tip events<br /> QHelpEvent '''helpEvent = static_cast&amp;lt;QHelpEvent'''&gt;(event); // Tool tip events come as the type QHelpEvent<br /> QPoint pos = helpEvent-&gt;pos(); // Get pos from event (instead of what was passed to maybeTip)


===Categories:===
// Code from maybeTip function. Modified for Qt 4.<br /> // Rather than tip() use QToolTip::showText() and QToolTip::hideText()<br /> //<br /> // For example, convert<br /> // tip(r, text);<br /> // to<br /> // QToolTip::showText(helpEvent-&gt;globalPos(), text, widget, r);


* [[:Category:HowTo|HowTo]]
return true; // Return true to filter event<br /> }<br /> return false; // Return false to allow other event processing<br />}<br /></code>
 
== Install event filter where custom tool tip class was used. ==
 
Change creation of custom tool tip<br /><code><br /> _toolTipper = new MyToolTipper(viewport(), this);<br /></code><br />to installing the event filter.<br /><code><br /> viewport()-&gt;installEventFilter(new MyToolTipper(viewport(), this));<br /></code>

Revision as of 11:29, 24 February 2015


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

<br />class MyToolTipper : public QToolTip<br />


to

<br />class MyToolTipper : public QObject<br />

Declare the eventFilter rather than maybeTip.

Change maybeTip

<br /> void maybeTip( const QPoint &amp;pos );<br />


to

<br />protected:<br /> bool eventFilter( QObject *obj, QEvent *event );<br />

Convert the maybeTip() implementation to eventFilter()

<br />bool MyToolTipper::eventFilter( QObject *obj, QEvent *event )<br />{<br /> if (event-&gt;type() == QEvent::ToolTip) { // Only process tool tip events<br /> QHelpEvent '''helpEvent = static_cast&amp;lt;QHelpEvent'''&gt;(event); // Tool tip events come as the type QHelpEvent<br /> QPoint pos = helpEvent-&gt;pos(); // Get pos from event (instead of what was passed to maybeTip)

// Code from maybeTip function. Modified for Qt 4.<br /> // Rather than tip() use QToolTip::showText() and QToolTip::hideText()<br /> //<br /> // For example, convert<br /> // tip(r, text);<br /> // to<br /> // QToolTip::showText(helpEvent-&gt;globalPos(), text, widget, r);

return true; // Return true to filter event<br /> }<br /> return false; // Return false to allow other event processing<br />}<br />

Install event filter where custom tool tip class was used.

Change creation of custom tool tip

<br /> _toolTipper = new MyToolTipper(viewport(), this);<br />


to installing the event filter.

<br /> viewport()-&gt;installEventFilter(new MyToolTipper(viewport(), this));<br />