PDA

View Full Version : How can we set tooltip to a particular area of widget?



pratik041
19th November 2011, 12:30
Suppose half of the area of widget we want to display tooltip and half area dont want to display. how can we do that?Is there any predefined function.

Zlatomir
19th November 2011, 13:27
I don't think you can do that, and i can't imagine why you would want to do it.

But what you can do is create your own "widget" (by deriving from QWidget or create it into the designer) and there you can have for example: a QLabel (with it's own tooltip) and a QLineEdit also with it's own tooltip... or two QLabel's and only one has a tooltip...

Also you can tell us more about what are you trying to achieve and maybe some of us have better ideas.

pratik041
19th November 2011, 13:51
I am creating a userdefined tooltip class and it has one function called class::add(Qwidget *w, QRect, Qstring);. I am passing following to the add fuction of the class while calling -address of object to which i want to set tooltip to QWidget pointer and Qrect is the rectangle portion of that object which i want to set tool tip and string is the tooltip text.
So my problem is that how to implement this tooltip class so that tooltip will be displayed only when the mouse pointer is inside that object rect. Actually i want to work all this when i call add function of tooltip class for any other widget.

d_stranz
19th November 2011, 18:49
Since I was just talking about event filters in another thread, it is still in my mind.

Tool tips are invoked through QHelpEvent types (in other words, QEvent::ToolTip uses a QHelpEvent instance). QHelpEvent contains the x, y position of the cursor. So, install an event filter on your widget, and examine the position to see if it is contained within the rect. If it is inside the rect, then you return "false" from the filter (which allows the event to go on to the widget), otherwise return "false" and the event is eaten.

If you make your user-defined tooltip class flexible enough, you could have multiple tool tips on a widget, each with its own rect. So, for example, if your widget is displaying a map, you could pop up a different tool tip for each city.

pratik041
21st November 2011, 05:50
Since I was just talking about event filters in another thread, it is still in my mind.

Tool tips are invoked through QHelpEvent types (in other words, QEvent::ToolTip uses a QHelpEvent instance). QHelpEvent contains the x, y position of the cursor. So, install an event filter on your widget, and examine the position to see if it is contained within the rect. If it is inside the rect, then you return "false" from the filter (which allows the event to go on to the widget), otherwise return "false" and the event is eaten.

If you make your user-defined tooltip class flexible enough, you could have multiple tool tips on a widget, each with its own rect. So, for example, if your widget is displaying a map, you could pop up a different tool tip for each city.


can you please give a brief code example for it. Does QHelpevent can be used to know the position of cursor of parent window in child window.

d_stranz
21st November 2011, 17:17
Read the docs. QHelpEvent has both global and widget coordinates. QWidget has mapFromGlobal() / mapToGlobal() and mapFromParent() / mapToParent() to translate point positions from any widget to any other widget.

Sorry, but I don't have a code example. This is not a hard problem to solve.