PDA

View Full Version : QTextCursor / replace selected text



migel
18th December 2012, 17:59
Can some one explain what I am doing wrong here ?. What I need is to select a text in QTextEdit and make some changes in the selected block texts.

debug prints selected block texts just fine but only when I remove "cursor.insertText("replace block text");". Once added back it screwing up somehow the block numbers and the entire concept. How to do it right ?



QTextCursor cursor = this->textCursor();
int start = cursor.selectionStart();
int end = cursor.selectionEnd();

if(!cursor.hasSelection())
return;

cursor.setPosition(start, QTextCursor::KeepAnchor);
int startLine = cursor.blockNumber();

cursor.setPosition(end, QTextCursor::KeepAnchor);
int lastLine = cursor.blockNumber();

qDebug() << start << end;

for(int blockNUmber = startLine; blockNUmber <= lastLine; blockNUmber++)
{
QTextBlock block = document()->findBlockByNumber(blockNUmber);
QString blockText = block.text();

qDebug() << blockNUmber << block.position() << blockText;

cursor.clearSelection();
cursor.setPosition(block.position(),QTextCursor::K eepAnchor);
cursor.insertText("replace block text");
}

pkj
19th December 2012, 05:07
First you are iterating over blocks in textdocument. So wouldn't this piece of documentation on QTextBlock should be taken care of?
The next() and previous() functions enable iteration over consecutive valid blocks in a document under the condition that the document is not modified by other means during the iteration process. Note that, although blocks are returned in sequence, adjacent blocks may come from different places in the document structure. The validity of a block can be determined by calling isValid().

migel
19th December 2012, 11:14
Thanks. I made some changes. In the example below the text is modified only in the first block. And again removing line cursor.intertText() fixes iteration.

What is wrong yet ? Appreciate for help !



QTextCursor cursor = this->textCursor();

int start = cursor.selectionStart();
int end = cursor.selectionEnd();

if(!cursor.hasSelection())
return;

cursor.setPosition(end, QTextCursor::KeepAnchor);
QTextBlock endBlock = cursor.block();

cursor.setPosition(start, QTextCursor::KeepAnchor);
QTextBlock block = cursor.block();

for(; block.isValid() && !(endBlock < block); block = block.next())
{
if (!block.isValid())
continue;

qDebug() << block.blockNumber() << block.position() << block.text();

cursor.clearSelection();
cursor.setPosition(block.position() ,QTextCursor::KeepAnchor);
cursor.insertText("replace block text");
}

migel
24th December 2012, 15:06
I found the problem. QTextCursor::setPosition() is not actually block position, so that was the issue. Best to operate using QTextCursor::movePosition()


QTextCursor cursor = textCursor();

int start = cursor.selectionStart();
int end = cursor.selectionEnd();

if(!cursor.hasSelection())
return;

cursor.setPosition(end, QTextCursor::KeepAnchor);
QTextBlock endBlock = cursor.block();

cursor.setPosition(start, QTextCursor::KeepAnchor);
QTextBlock block = cursor.block();

for(; block.isValid() && !(endBlock < block); block = block.next())
{
if (!block.isValid())
continue;

cursor.movePosition(QTextCursor::StartOfLine);
cursor.clearSelection();
cursor.insertText(" text ");
cursor.movePosition(QTextCursor::NextBlock);
}