Results 1 to 5 of 5

Thread: Unexpected QTextCursor behaviour

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Nov 2014
    Location
    Germany
    Posts
    69
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Unexpected QTextCursor behaviour

    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:
    Qt Code:
    1. this->blockSignals(true);
    2.  
    3. QTextCursor cursorCopy = edit->textCursor();
    4. int intCursorPosPre = edit->textCursor().position();
    5. QMessageBox::information(this, "Before changing QTextDocument", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
    6. this->setPlainText(stringText);
    7. QMessageBox::information(this, "After changing QTextDocument", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
    8. if(intTextLengthPre == intTextLengthPost)
    9. cursorCopy.setPosition(intCursorPosPre);
    10. else
    11. cursorCopy.setPosition(intCursorPosPre-1);
    12. QMessageBox::information(this, "After setting previously saved position to cursorCopy", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
    13. edit->setTextCursor(cursorCopy);
    14. QMessageBox::information(this, "After setting cursorCopy to cursorOriginal", QString::number(edit->textCursor().position()) + "\n" + QString::number(cursorCopy.position()));
    15.  
    16. 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:
    Qt Code:
    1. 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:
    Qt Code:
    1. 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:
    Qt Code:
    1. 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!
    Last edited by Binary91; 10th July 2015 at 12:58.

Similar Threads

  1. Replies: 1
    Last Post: 28th February 2014, 16:01
  2. Link Error and Unexpected Finish
    By decoy in forum Newbie
    Replies: 1
    Last Post: 14th June 2012, 01:43
  3. Unexpected behavior with commits and rollbacks...
    By mtnbiker66 in forum Qt Programming
    Replies: 6
    Last Post: 6th June 2012, 15:06
  4. Unexpected build errors in a Qt project
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2010, 21:33
  5. QTabWidget->setCornerWidget() unexpected resize.
    By hickscorp in forum Qt Programming
    Replies: 7
    Last Post: 20th December 2006, 14:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.