Results 1 to 3 of 3

Thread: Unicode problem in hoverlink

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Unicode problem in hoverlink

    Hi ,
    I am having a little problem with the links. I got his code from someone, which is used to give a hover effect on the links. The code is running fine on ASCII charactes, but goes haywire while using unicode characters, the problem seems to be that complex characters are built using more then one characters and hence the code so not show the whole text as underlined and infact breaks the characters on hover. I am not able to find the solution to this problem. Kindly help me. I am pasting the code below:

    Qt Code:
    1. class HoverLinkEnabler_C : public QObject {
    2.  
    3. QTextEdit *_editor;
    4. QTextCursor _last_cursor;
    5.  
    6. public:
    7.  
    8. HoverLinkEnabler_C(QTextEdit *editor) :
    9. _editor(editor)
    10. {
    11. _editor->viewport()->installEventFilter(this);
    12. _editor->viewport()->setMouseTracking(true);
    13. }
    14.  
    15. virtual bool eventFilter(QObject *watched, QEvent *event)
    16. {
    17. if ( watched == _editor->viewport() ) {
    18. if ( event->type() == QEvent::MouseMove ) {
    19.  
    20. QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);
    21. QString anchor = _editor->anchorAt(mouse_event->pos());
    22.  
    23. if ( anchor.isEmpty() ) {
    24. if ( ! _last_cursor.isNull() ) {
    25. EndHover(_last_cursor);
    26. _last_cursor = QTextCursor();
    27. }
    28. } else {
    29. if ( _last_cursor.isNull() ) {
    30. QTextCursor mouse_cursor = _editor->cursorForPosition(mouse_event->pos());
    31. QTextCursor link_cursor = GetLinkAt(mouse_cursor);
    32. StartHover(link_cursor);
    33. _last_cursor = link_cursor;
    34. }
    35. }
    36.  
    37. } else if ( event->type() == QEvent::Leave ) {
    38.  
    39. if ( ! _last_cursor.isNull() ) {
    40. EndHover(_last_cursor);
    41. _last_cursor = QTextCursor();
    42. }
    43.  
    44. }
    45.  
    46. }
    47.  
    48. return QObject::eventFilter(watched, event);
    49. }
    50.  
    51. private:
    52.  
    53. QTextCharFormat GetFormatOf(QTextCursor cursor, bool skip_first = false)
    54. {
    55. if ( skip_first && !cursor.atBlockEnd() ) {
    56. cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
    57. }
    58. return cursor.charFormat();
    59. }
    60.  
    61. QTextCursor GetLinkAt(QTextCursor cursor)
    62. {
    63. QTextCharFormat fmt = GetFormatOf(cursor, true);
    64.  
    65. int first_pos = cursor.position();
    66. while ( !cursor.atBlockStart() && cursor.charFormat() == fmt && cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor) ) {
    67. first_pos--;
    68. }
    69.  
    70. int last_pos = cursor.position();
    71. while ( !cursor.atBlockEnd() && cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor) && cursor.charFormat() == fmt ) {
    72. last_pos++;
    73. }
    74.  
    75. cursor.setPosition(first_pos, QTextCursor::MoveAnchor);
    76. cursor.setPosition(last_pos, QTextCursor::KeepAnchor);
    77. return cursor;
    78. }
    79.  
    80. void StartHover(QTextCursor hover_cursor)
    81. {
    82. QTextCharFormat fmt = GetFormatOf(hover_cursor);
    83. fmt.setUnderlineStyle(QTextCharFormat::SingleUnderline);
    84. hover_cursor.setCharFormat(fmt);
    85. }
    86.  
    87. void EndHover(QTextCursor hover_cursor)
    88. {
    89. QTextCharFormat fmt = GetFormatOf(hover_cursor);
    90. fmt.setUnderlineStyle(QTextCharFormat::NoUnderline);
    91. hover_cursor.setCharFormat(fmt);
    92. }
    93.  
    94. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Unicode problem in hoverlink

    Set an utf8 text codec for strings before using that class:
    Qt Code:
    1. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    To copy to clipboard, switch view to plain text mode 

    Regards

  3. #3
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unicode problem in hoverlink

    Well When I searched the forum for similar problem , I got the same solution but this is not working. It seems that we are using this API to display the text on the screen, but in my case everything is coming fine on the screen, only the hover link code seems to be workin incorrectly on the complex characters

Similar Threads

  1. Unicode Character Problem
    By prakash in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2006, 07:25
  2. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.