How to show Tooltips in Qt
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 开发者_StackOverflowwant 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).
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.
If you only use simple HTML, you can switch to a QTextBrowser. Then use the cursorForPosition method to get a QTextCursor from which you can get the text at the current location.
精彩评论