January 5, 2012

bmanc bmanc
Lab Rat
13 posts

How do I find the first and (more important) the last QTextBlock in a selection provided by QTextCursor?


I have a QTextEdit, and in it I want to determine the first and last selected block in the selection associated with the cursor returned by QTextEdit::textCursor().

So far I don’t see anything in the API that lets me do this.


2 replies

January 6, 2012

joonhwan joonhwan
Lab Rat
83 posts

  1. QTextCursor cursor = textEdit->textCursor();
  2. qDebug() << "your selection : " << cursor.selectionStart() << "~" << cursor.selectionEnd();

Hope this helps.


joonhwan at gmail dot com

January 6, 2012

bmanc bmanc
Lab Rat
13 posts

Thanks for the reply. Unfortunately I’m not sure that helps what I’m aiming. Sorry for not going into depth about the reason earlier.

I’m actually trying to get the QTextBlock. The reason is for bulleted list management. I want to determine the text blocks that are covered by the user’s selection. So if they have parts of a bulleted lists selected (could be across different levels of indentation) I can increment/decrement the indentation of each selected block without affecting the non-selected portions of the list.

Your suggestion worked for me. Using the selectionStart() and selectionEnd() methods you pointed out along with QTextDocument::findBlock(pos) I got what I needed.

  1.     QTextCursor c = textCursor();
  2.     QTextBlock b1 = document()->findBlock(c.selectionStart());
  3.     QTextBlock b2 = document()->findBlock(c.selectionEnd());

  ‹‹ doubt about deleting a child QObject      Is there a way to customize/Replace QMdiArea’s QTabBar which shows opened Tabs list like in FireFox? ››

You must log in to post a reply. Not a member yet? Register here!