Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cloned QTextDocument function failure
Forum Updated to NodeBB v4.3 + New Features

Cloned QTextDocument function failure

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 886 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pt_develop
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pt_develop
      wrote on last edited by
      #2

      Oh no, another stupid mistake by me. I accidently used the QTextEdit find() prototype rather than the QTextDocument prototype. Changing the while loop to include the cursor fixed the problem. For example the secondClone while loop should be:

      @ while(!(cur=secondClone->find("K",cur,QTextDocument::FindCaseSensitively)).isNull())@

      rather than
      @ while(!(cur=secondClone->find("K",QTextDocument::FindCaseSensitively)).isNull())@

      Problem solved!

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved