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
(Decode HTML entity names)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:HowTo]]
[[Category:HowTo]]


Line 7: Line 9:
== Derive from QObject instead QToolTip ==
== Derive from QObject instead QToolTip ==


Change<br /><code><br />class MyToolTipper : public QToolTip<br /></code><br />to<br /><code><br />class MyToolTipper : public QObject<br /></code>
Change
<code>
class MyToolTipper : public QToolTip
</code>
to
<code>
class MyToolTipper : public QObject
</code>


== Declare the eventFilter rather than maybeTip. ==
== Declare the eventFilter rather than maybeTip. ==


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>
Change maybeTip
<code>
void maybeTip( const QPoint &pos );
</code>
to
<code>
protected:
bool eventFilter( QObject *obj, QEvent *event );
</code>


== Convert the maybeTip() implementation to eventFilter() ==
== Convert the maybeTip() implementation to eventFilter() ==


<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)
<code>
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.<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);
// 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<br /> }<br /> return false; // Return false to allow other event processing<br />}<br /></code>
return true; // Return true to filter event
}
return false; // Return false to allow other event processing
}
</code>


== Install event filter where custom tool tip class was used. ==
== 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>
Change creation of custom tool tip
<code>
_toolTipper = new MyToolTipper(viewport(), this);
</code>
to installing the event filter.
<code>
viewport()->installEventFilter(new MyToolTipper(viewport(), this));
</code>

Latest revision as of 17:42, 12 March 2015

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