While adding headers and footers to my printouts I ran across the following rather courious problem. When I cloned the clone of a QTextDocument several of the QTextDocument functions failed. The folliwing code fragnent illustrates the problem
int pmax=5;
int pc=0;
QTextEdit *myedit = new QTextEdit;
myedit->setPlainText(QString("__K__ of __N__"));
QTextDocument *firstClone=myedit->document()->clone();
{ // limit the scope of cur for debug
QTextCursor cur(firstClone);
while(!(cur=firstClone->find("__N__",QTextDocument::FindCaseSensitively)).isNull())
cur.insertText(QString::number(pmax));
qDebug()<<"first clone text:"<<firstClone->toPlainText();
}
QTextDocument *secondClone=firstClone->clone();
{ //limit the scope of cur for debug
QTextCursor cur(secondClone);
while(!(cur=secondClone->find("__K__",QTextDocument::FindCaseSensitively)).isNull())
cur.insertText(QString::number(pc));
qDebug()<<"second clone text:"<<secondClone->toPlainText();
}
/*
QTextDocument *secondClone=firstClone->clone();
QString temp=secondClone->toPlainText();
temp.replace("__K__",QString::number(pc));
secondClone->setPlainText(temp);
qDebug()<<"second clone text:"<<secondClone->toPlainText();
*/
The find function works perfectly on the first clone of myedit but fails on the second clone of myedit (i.e., the second clone is a clone of the first clone).
Since I am using plain text the commented out code is a viable work around but that will not always be the case so I’d like to know know if this is a bug or a misunderstanding on my part. I’d appreciate any feedback that can shed some light on this behavior.
↧