Results 1 to 4 of 4

Thread: How to select a email in QTextEdit

  1. #1
    Join Date
    Sep 2017
    Posts
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default How to select a email in QTextEdit

    I want to select the email in a QTextEdit widget and then highlight each email . But I found that select() funciton only support 4 types: Document, BlockUnderCursor, LineUnderCursor and WordUnderCursor.

    So, is there any other way can only select an email?

    My input example in QTextEdit is

    email@abc.com , b@acb.com

    I know that I can use QTextCursor to select the text .

    Qt Code:
    1. QTextCursor tc = ui->textEdit->textCursor();
    2. tc.select(QTextCursor::WordUnderCursor);
    3. QString text = tc.selectedText();
    To copy to clipboard, switch view to plain text mode 

    using email@abc.com, b@acb.com as an example, it would return email , abc, com , b , acb ,com.
    I want to return the text like email@abc.com and b@acb.com.

    How can I do that? Please give me some helps. Thanks!
    Last edited by ng2b30; 11th April 2018 at 07:15.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to select a email in QTextEdit

    I am not sure what you mean by "select".
    Do you mean highlight it?
    Or extract is from the rest of the text?
    For both cases have a look at QRegExp http://doc.qt.io/qt-5/qregexp.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,307
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to select a email in QTextEdit

    For both cases have a look at QRegExp http://doc.qt.io/qt-5/qregexp.html
    I looked at that, and the QRegularExpression equivalent for QTextDocument::find(). All of the find() methods return a QTextCursor pointing to the first position in the found string (if any). Then what? I think the OP is having the same problem that I could see at that point - there is no way to retrieve the full text fragment that was matched by the regexp and highlight (select) it. QTextCursor gives you four ways to select text: whole document, block under cursor, line under cursor, or word under cursor.

    None of these will work for a URL - as I think the OP is seeing, "WordUnderCursor" breaks at the first non-character position, which is the "@" symbol in his examples.

    So the bottom line question is, how do you find and highlight an arbitrary string in a QTextDocument?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to select a email in QTextEdit

    I looked at that, and the QRegularExpression equivalent for QTextDocument::find(). All of the find() methods return a QTextCursor pointing to the first position in the found string (if any). Then what? I think the OP is having the same problem that I could see at that point - there is no way to retrieve the full text fragment that was matched by the regexp and highlight (select) it. QTextCursor gives you four ways to select text: whole document, block under cursor, line under cursor, or word under cursor.
    It seems I have trouble understanding what the problem is...
    In the case of a mail address, you could use the word under cursor option - don't you agree?

    None of these will work for a URL - as I think the OP is seeing, "WordUnderCursor" breaks at the first non-character position, which is the "@" symbol in his examples.
    Are you sure it's break at first non-character and not at first white space?
    I don't have the time to check, but if you are right, I am very surprised.

    So the bottom line question is, how do you find and highlight an arbitrary string in a QTextDocument?
    OK, now that is a clear question! :-)

    Did you guys have a look at QSyntaxHighlighter ?
    I used it some years back and had no trouble highlighting text - but for any more details I will have to look in again...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QTextEdit copy and paste into email.
    By chris_helloworld in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2010, 09:17
  2. How to select a tex in QTExtEdit
    By Furkan in forum Newbie
    Replies: 3
    Last Post: 12th September 2010, 19:00
  3. How to send email and attachement via default email client
    By richardander in forum Qt Programming
    Replies: 3
    Last Post: 1st December 2009, 05:40
  4. How to tokenize email address in QTextEdit?
    By blonde in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 15:34
  5. Replies: 7
    Last Post: 5th January 2009, 09:27

Tags for this Thread

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.