Qt3ToQt4 How to convert custom tool tip to an event filter: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
AutoSpider (talk | contribs) (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 | 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 | 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> | <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. | // 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 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 | 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));