PDA

View Full Version : Tooltip Displaying on the wrong position



nekkro-kvlt
18th February 2012, 15:22
Hi all,

I have a weird issue. I'm trying to display a tooltip on a QPlainTextEdit subclass, and the tooltip is shown too much above the mouse cursor. I'm currently showing the word under the mouse cursor. I basically used the code here:
http://developer.qt.nokia.com/faq/answer/how_can_i_display_a_tooltip_over_only_one_word_in_ a_qlabel



bool logEditor::event(QEvent *event)
{
if (event->type() == QEvent::ToolTip)
{
processTooltip(static_cast <QHelpEvent*>(event)->pos(),static_cast <QHelpEvent*>(event)->globalPos());
return true;
}

return QPlainTextEdit::event(event);
}

void logEditor::processTooltip(QPoint pos, QPoint globalpos)
{
QPoint adjustedPos=pos;
adjustedPos.setX(adjustedPos.x()-lineNumberAreaWidth());
QTextCursor cursor = cursorForPosition(adjustedPos);
cursor.select(QTextCursor::WordUnderCursor);
QToolTip::showText(globalpos, cursor.selectedText());

}



And see the result:
7414

The word displayed is the correct word, but the position is wrong...

norobro
18th February 2012, 21:31
I'm not sure I understand what the problem is. From your screen shot it looks like the toolTip is shown in the standard position.


. . . but the position is wrong...
You can adjust the position. See QPoint::rx(). For example, to move it closer to the cursor vertically try this in processTootip():
globalpos.ry() -= 5

nekkro-kvlt
19th February 2012, 18:26
Yes I know I can adjust it, but I was just wondering if i was doing something wrong, since afaik the global pos of the event should be right under the cursor ... Here I have a space of approx 1 cursor between the cursor and the tooltip...

Also, do you know how to reduce the delay for the tooltip to show ?

Thanks :)

norobro
19th February 2012, 20:22
From QToolTip::showText():
The tool tip will be shown with a platform specific offset from this point of interest.
And from the source code (http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/kernel/qtooltip.cpp#line385):
QPoint p = pos;
p += QPoint(2,
#ifdef Q_WS_WIN
21
#else
16
#endif
);

Also, do you know how to reduce the delay for the tooltip to show ?
Sorry, can't help with that.

nekkro-kvlt
20th February 2012, 09:29
That Explain then, thanks !