Results 1 to 6 of 6

Thread: Problem of highlighting text in QPlainTextEdit

  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    Hello,

    My Qt SDK version 4.7.4

    I use QPlainTextEdit::setExtraSelections() to highlight some words in QPlainTextEdit. Following is my code:

    Qt Code:
    1. int pos = block.position() + p;
    2.  
    3. QTextEdit::ExtraSelection selection;
    4. selection.cursor = QTextCursor(ui->plainTextEdit->document());
    5. selection.cursor.setPosition( pos );
    6. selection.cursor.setPosition( pos+highlighText.length(), QTextCursor::KeepAnchor );
    7. selection.format.setBackground( backBrush );
    8. selection.format.setForeground( textBrush );
    9. selection.format.setProperty( QTextFormat::OutlinePen, outlinePen );
    10.  
    11. extraSelections.append( selection );
    To copy to clipboard, switch view to plain text mode 

    Everything seems Ok. But when I move the caret on the position of highlighted text. The highlighted text is not diaplay ok. Please refer to the screenshot.

    2012-4-17 11-27-39.png


    Added after 4 minutes:


    I attached a demo for the problem.

    Can anybody help me?
    Attached Files Attached Files
    Last edited by topfortune; 17th April 2012 at 04:48.

  2. #2
    Join Date
    Sep 2011
    Location
    Mannheim, Germany
    Posts
    22
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    maybe you can force periodical the paint event. It looks like Qt <-> WinXP problem. I have got a similar problem with QPlaintextEdits on some Windows machines with Qt 4.5.3.

  3. #3
    Join Date
    Nov 2011
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    Quote Originally Posted by dennis81 View Post
    maybe you can force periodical the paint event. It looks like Qt <-> WinXP problem. I have got a similar problem with QPlaintextEdits on some Windows machines with Qt 4.5.3.
    I built and run the demo on Ubuntu. And the problem also exists on Linux. I think it must be a bug of Qt. I really don't want to modify the source code of Qt for the problem.

  4. #4
    Join Date
    Nov 2011
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    Can anybody help me? Please....

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    Hey,

    I appreciate your effort to prepare the example, in return (and because I had some spare time) I've got solution for you

    It seems in fact to be a bug in Qt, from what I've found the paint event for QPlainTextEdit contains wrong rectangle.

    You can fix it yourself without affecting Qt source, here's how:

    Qt Code:
    1. class FixedQPlainTextEdit : public QPlainTextEdit // I guess the name tells you everything :)
    2. {
    3. public:
    4. FixedQPlainTextEdit( QWidget* parent = 0 ) : QPlainTextEdit( parent ) {}
    5.  
    6. protected:
    7. void paintEvent( QPaintEvent* e )
    8. {
    9. QPaintEvent* fixed_event = new QPaintEvent( e->rect().adjusted( -1, -1, 0, 0 ) ); // left and top edge need to be adjusted by -1
    10. QPlainTextEdit::paintEvent( fixed_event );
    11. delete fixed_event;
    12. }
    13. };
    14.  
    15. // below is your example code using the FixedQPlainTextEdit class
    16. MainWindow::MainWindow( QWidget* p )
    17. :
    18. {
    19. QPlainTextEdit* e = new FixedQPlainTextEdit();
    20. e->appendPlainText( "#include <QtGui/QApplication>" );
    21. e->appendPlainText( "#include \"mainwindow.h\"" );
    22. e->appendPlainText( "" );
    23. e->appendPlainText( "int main(int argc, char *argv[])" );
    24. e->appendPlainText( "{" );
    25. e->appendPlainText( " QApplication a(argc, argv);" );
    26. e->appendPlainText( " MainWindow w;" );
    27. e->appendPlainText( " w.show();" );
    28. e->appendPlainText( "" );
    29. e->appendPlainText( " return a.exec();" );
    30. e->appendPlainText( "}" );
    31.  
    32. this->setCentralWidget( e );
    33.  
    34. QList<QTextEdit::ExtraSelection> extraSelections;
    35.  
    36. QBrush backBrush( Qt::yellow );
    37. QBrush textBrush( Qt::black );
    38. QPen outlinePen( Qt::gray, 1 );
    39. QString highlighText = "QApplication";
    40.  
    41. for( int i=0; i<e->document()->blockCount(); i++ )
    42. {
    43. QTextBlock block = e->document()->findBlockByNumber( i );
    44. if( block.isValid() )
    45. {
    46. QString text = block.text();
    47. int p;
    48.  
    49. if( (p=text.indexOf(highlighText)) != -1 )
    50. {
    51. int pos = block.position() + p;
    52.  
    53. QTextEdit::ExtraSelection selection;
    54. selection.cursor = QTextCursor(e->document());
    55. selection.cursor.setPosition( pos );
    56. selection.cursor.setPosition( pos+highlighText.length(), QTextCursor::KeepAnchor );
    57. selection.format.setBackground( backBrush );
    58. selection.format.setForeground( textBrush );
    59. selection.format.setProperty( QTextFormat::OutlinePen, outlinePen );
    60.  
    61. extraSelections.append( selection );
    62. }
    63. }
    64. }
    65.  
    66. e->setExtraSelections( extraSelections );
    67. }
    To copy to clipboard, switch view to plain text mode 

    Edit:
    Btw the issue was also visible if you placed the cursor under the highlighted field.

  6. #6
    Join Date
    Nov 2011
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of highlighting text in QPlainTextEdit

    Hi Spitfire,

    It's perfect! Thank you so much for your solution. Thank you so much!!!

Similar Threads

  1. multicolor text in QPlainTextEdit
    By mastupristi in forum Qt Programming
    Replies: 5
    Last Post: 14th January 2011, 21:37
  2. Selecting Text QPlainTextEdit
    By smhall316 in forum Newbie
    Replies: 0
    Last Post: 7th December 2010, 20:09
  3. read text from QPlainTextEdit
    By chs in forum Newbie
    Replies: 2
    Last Post: 21st November 2009, 12:07
  4. Highlighting the keywords in a text edit
    By divya balachandran in forum Qt Programming
    Replies: 3
    Last Post: 6th September 2008, 14:25
  5. highlighting text between the same symbols
    By kib2 in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2007, 21:53

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.