PDA

View Full Version : How to select a email in QTextEdit



ng2b30
11th April 2018, 06:08
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 .


QTextCursor tc = ui->textEdit->textCursor();
tc.select(QTextCursor::WordUnderCursor);
QString text = tc.selectedText();

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!

high_flyer
15th April 2018, 23:48
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

d_stranz
17th April 2018, 16:16
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?

high_flyer
19th April 2018, 13:31
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...