Results 1 to 13 of 13

Thread: QTextEdit and mark Text

  1. #1

    Default QTextEdit and mark Text

    Hi,

    I want to code a search, like the text search in FireFox.
    And I've got a problem with highlighting the text. I know that the background color can be changed with QTextCharFormat::setBackground(QBrush) - but how can I do that with a part of the document and not with the whole?

    My Idea was like
    Qt Code:
    1. QTextCursor cursor = this->textCursor();
    2. cursor.movePosition(QTextCursor::StartOfLine);
    3. cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
    4. QTextCharFormat format = cursor.charFormat();
    5. format.setBackground(QBrush(Qt::green));
    6. cursor.setCharFormat(format);
    To copy to clipboard, switch view to plain text mode 
    But nothing happens. When putting the format into the QTextEdit, the whole document has the background color green.

    The next problem should be, finding the Textposition in the document. But first this one ...

    EDIT: I forgot to call QTextEdit::setTextCursor ... After calling this, the document is completly green in the background ...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    You are highlighting the whole line here. Is that what you want? Maybe the content of the text edit is a single line?

  3. #3

    Default Re: QTextEdit and mark Text

    No, it has more than 20 lines in the test. I want to highlight one line, but all lines are highlighted here, that is my problem ... And I've got no idea, why ...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    This works for me:
    Qt Code:
    1. #include <QTextEdit>
    2. #include <QApplication>
    3. #include <QTextDocument>
    4. #include <QTextCursor>
    5.  
    6.  
    7.  
    8. int main(int argc, char **argv){
    9. QApplication app(argc, argv);
    10. QString str;
    11. for(int i=0;i<20;i++) str+=QString("Line%1\n").arg(i+1);
    12. doc.setPlainText(str);
    13. te.setDocument(&doc);
    14. QTextCursor cur = te.textCursor();
    15. do{
    16. cur = doc.find("7", cur);
    17. if(!cur.isNull()){
    18. cur.select(QTextCursor::LineUnderCursor);
    19. QTextCharFormat format = cur.charFormat();
    20. format.setBackground(Qt::green);
    21. cur.setCharFormat(format);
    22. }
    23. } while(!cur.isNull());
    24. te.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: QTextEdit and mark Text

    Your example works fine. But where is the problem here?
    Qt Code:
    1. void QIDTextEdit::highlightText()
    2. {
    3. QTextCursor cursor = this->textCursor();
    4. cursor = this->document()->find("main", cursor);
    5. cursor.select(QTextCursor::LineUnderCursor);
    6. QTextCharFormat format = cursor.charFormat();
    7. format.setBackground(Qt::green);
    8. cursor.setCharFormat(format);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Nothing happens ... The function is executet normally and "main" is in the text of the document ...

  6. #6

    Default Re: QTextEdit and mark Text

    Anyone got an hint for this problem?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    Does find() return a valid cursor?

  8. #8

    Default Re: QTextEdit and mark Text

    Hmm ... No ... But this->document()->toPlainText(); returns the corrent text - with the word "main" ...

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    So either the text is incorrect despite what you may think or the initial cursor position is after the last occurance of the term you seek.

  10. #10

    Default Re: QTextEdit and mark Text

    I don't understand what you mean ...

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    find() can fail (return a null cursor) in two cases - either the searched term doesn't exist in the text or it exists but you told find() to skip the part of text where the searched term exists (that's what the cursor argument to find() is for).

  12. #12

    Default Re: QTextEdit and mark Text

    And how can I tell find() not to skip the part?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit and mark Text

    Pass it an empty cursor or one that is positioned at the beginning of the text.

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.