Hi,
at first, thank you for you explanations!
Well, if I understand you right, QTextDocument is the "mother" or "parent" of QTextCursor and the original QTextCursor has a pointer to it and by copying it to my new QTextCursor I also copy the pointer to QTextDocument, so the "mystic" connection goes through QTextDocument.
That would make sense, if the copy exactly changed its position like the original QTextCursor (what I thought till a view minutes ago).
Let's take the example again:
this->blockSignals(true);
int intCursorPosPre = edit->textCursor().position();
QMessageBox::information(this,
"Before changing QTextDocument",
QString::number(edit
->textCursor
().
position()) + "\n" + QString::number(cursorCopy.
position()));
this->setPlainText(stringText);
QMessageBox::information(this,
"After changing QTextDocument",
QString::number(edit
->textCursor
().
position()) + "\n" + QString::number(cursorCopy.
position()));
if(intTextLengthPre == intTextLengthPost)
cursorCopy.setPosition(intCursorPosPre);
else
cursorCopy.setPosition(intCursorPosPre-1);
QMessageBox::information(this,
"After setting previously saved position to cursorCopy",
QString::number(edit
->textCursor
().
position()) + "\n" + QString::number(cursorCopy.
position()));
edit->setTextCursor(cursorCopy);
QMessageBox::information(this,
"After setting cursorCopy to cursorOriginal",
QString::number(edit
->textCursor
().
position()) + "\n" + QString::number(cursorCopy.
position()));
this->blockSignals(false);
this->blockSignals(true);
QTextCursor cursorCopy = edit->textCursor();
int intCursorPosPre = edit->textCursor().position();
QMessageBox::information(this, "Before changing QTextDocument", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
this->setPlainText(stringText);
QMessageBox::information(this, "After changing QTextDocument", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
if(intTextLengthPre == intTextLengthPost)
cursorCopy.setPosition(intCursorPosPre);
else
cursorCopy.setPosition(intCursorPosPre-1);
QMessageBox::information(this, "After setting previously saved position to cursorCopy", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
edit->setTextCursor(cursorCopy);
QMessageBox::information(this, "After setting cursorCopy to cursorOriginal", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
this->blockSignals(false);
To copy to clipboard, switch view to plain text mode
Well, I found out, that when "stringText" did not change in prior algorithms (hence, QTextEdit is updated with the same text it had before), then the following:
edit->setPlainText(QString);
To copy to clipboard, switch view to plain text mode
only changes the cursorOriginal (it sets it to zero). The cursorCopy is not changed, hence its position is always the same and I don't need to save it into an int...
Instead, when "stringText" changes in prior algorithms (e.g. it was shorten from length 10 to length 9 characters), then:
edit->setPlainText(QString);
To copy to clipboard, switch view to plain text mode
sets the cursorOriginal to zero again, BUT the cursorCopy increments by 1!
Can you explain this behaviour to me? It seems that it is not that easy that both, the cursorOriginal and the cursorCopy, change identically...
EDIT:
Ah after trying some constellations, I detected the rule in this behaviour:
After setting new text:
edit->setPlainText(QString);
To copy to clipboard, switch view to plain text mode
the cursorOriginal always sets to zero and the cursorCopy always sets to the last possible position!! 

But I still don't see a reason or a sense behind this behaviour!
Bookmarks