tags:

views:

657

answers:

2

I'm writing a program using Qt4.5 that shows a book in another language. So I have a large body of text and I want to display a different tooltip for each word (the tooltip will contain parsing information for the appropriate word).

What is the best way of doing this?

I have a working solution for a browser using php and jquery so I had considered porting it somehow to c++ and displaying it using QWebView but I have a feeling that there's a better solution out there...

[edit] Let me add that I display the words out of a database so I have a database that looks like this:

| Id | Word | ParsingInfo                  |
--------------------------------------------
|  0 | The  | Article                      |
|  1 | fish | Noun, Subject etc.           |
|  2 | are  | Verb, 3rd Person Singular... |
|  3 | blue | Adjective...                 |

So I need some way of figuring out what the id is of the word being displayed, not merely the word because, for example, the word "that" can be used in different ways or "wind" has a homograph (the language I am working with is greek though).

+1  A: 

From the QToolTip documentation:

The simplest and most common way to set a widget's tool tip is by calling its QWidget::setToolTip() function.

It is also possible to show different tool tips for different regions of a widget, by using a QHelpEvent of type QEvent::ToolTip. Intercept the help event in your widget's event() function and call QToolTip::showText() with the text you want to display. The Tooltips example illustrates this technique.

You will have to determine the word that is under the cursor when you get the help event. I don't know offhand how to do that, but I'm sure there is some way.

Caleb Huitt - cjhuitt
Thanks, that is the problem that I am struggling with though - figuring out which word is under the cursor...
j3frea
A: 

If you only use simple HTML, you can switch to a QTextBrowser (http://doc.trolltech.com/4.6/qtextbrowser.html). Then use the cursorForPosition (http://doc.trolltech.com/4.6/qtextedit.html#cursorForPosition) method to get a QTextCursor from which you can get the text at the current location.

e8johan
This looks interesting, thanks, how would I go about getting the Id of the word though?
j3frea
@j3frea: Using a QTextEdit or QTextBrowser, you could build up your string using QTextBlocks. Each block (a word, in your case) has the ability to store user data (your ID). Then the cursorForPosition function mentioned here can get a QTextCursor, which returns a QTextBlock for the block it is in, and you could get your user data (the ID) from that. It is somewhat convoluted, though.
Caleb Huitt - cjhuitt
Thanks very much that looks like the solution that I'm looking for...
j3frea