I believe everyone has used a dictionary! Because English is not very good ... O (∩_∩) o~, so often the translation of the word!
- Briefly
- Realize
- More references
Realize
Principle: Move the mouse over a word, get the mouse position, and then in the corresponding position to take the word, translation!
Based on this principle, we implement a qtooltip for each word.
Effect
Source
Create Qtextedit formations, and then listen to events through Installeventfilter.
M_ptextedit= new qtextedit (this); M_ptextedit- >setobjectname ( "Highlightlabel"); M_ptextedit->append ( Qstring::fromlocal8bit ( "one go, two or three Miles"); m_ptextedit ->append (Qstring::fromlocal8bit ( " Youth is not old, struggle not only! ")); M_ptextedit->append (Qstring::fromlocal8bit (" You were not alone. ")); M_ptextedit->append (Qstring::fromlocal8bit ( //Install event filter M_ptextedit->installeventfilter (this);
Implement EventFilter, Judge the event type is Qevent::tooltip, get the cursor, take the word.
BOOL MainWindow:: EventFilter (Qobject*obj, Qevent*event) {if (obj= = M_ptextedit) {Event is a hintif (event-Type ()= = Qevent:: ToolTip) {qhelpevent*phelpevent = static_cast<qhelpevent *> (event); //Get cursor qtextcursor cursor = m_ptextedit->cursorforposition (phelpevent->pos ()); cursor. Select (Qtextcursor:: wordundercursor);//real participle //Display message Qtooltip:: Showtext (Phelpevent- Globalpos (), cursor. SelectedText ()); return true;}} return qdialog:: EventFilter (obj, event);}
If you are unfamiliar with event filtering, see more references.
http://blog.csdn.net/liang19890820/article/details/51804098
Qobject have eventfilter, function very strong (arbitrary processing, such as to qlineedit participle)