How to find a line number after an Enter key press in QTextEdit.
Solved
General and Desktop
-
Say I have some lines in QTextEdit.
Line number 1
Line number 2
Line number 3Now I press an Enter key after Line number 2 and it looks like this now
Line number 1
Line number 2
'\r'
Line number 3Now I wont to get a line before the Enter key - Line number 2
So I can do like this
switch (keyEvent->key()) { case Qt::Key_Return: { if (term_str.isEmpty()) { QTextDocument *doc = ui->textEditTerminalTx->document(); QTextBlock tb = doc->findBlockByLineNumber(???); QString s = tb.text(); } } break; }
But how do I get a line number to put in doc->findBlockByLineNumber(???) ?
-
-
To retrieve the block under the cursor, you can do:
QTextDocument *doc = ui->textEditTerminalTx->document(); //QTextCursor cursor=doc->textCursor(); QTextCursor cursor=ui->textEditTerminalTx->textCursor(); QTextBlock tb = cursor.block();
@mpergand said in How to find a line number after ann Enter key in QTextEdit.:
To retrieve the block under the cursor, you can do:
QTextDocument *doc = ui->textEditTerminalTx->document(); QTextCursor cursor=doc->textCursor(); QTextBlock tb = cursor.block();
I need the line above the cursor - only one line above.
-
int QTextCursor::blockNumber() const
Returns the number of the block the cursor is in, or 0 if the cursor is invalid.