QTextEdit.selectAll() and clearSelection().



  • I'm trying to change the font size used in a QTextEdit object after the text has been loaded. My problem is how to un-select everything afterwards.

    Changing the font size is easy:
    @QTextEdit theText;
    // Load text into theText and allow the user to edit it.
    theText.selectAll();
    theText.setFontPointSize(size);@

    but the text is shown in xor mode to indicate that it is selected. To bring the state of theText back to what it was before changing the font size, I've tried this
    @QTextCursor theCursor = theText.textCursor();
    theCursor.clearSelection();@

    I've also tried
    @theCursor.movePosition(QTextCursor::NoMove,QTextCursor::MoveAnchor);@

    and
    @theCursor.movePosition(QTextCursor::Start,QTextCursor::MoveAnchor);@

    and various other ways of moving the cursor after the change to the font size. Nothing seems to have any effect.

    If it matters, when theText was created, I called
    @theText.setAcceptRichText(false);@

    So the text is being displayed in plainest possible manner.



  • Try this:
    @
    textEdit = new QTextEdit(tr("This is sample data"));

    QTextCursor cursor = textEdit->textCursor();
    textEdit->selectAll();
    textEdit->setFontPointSize( 20.0 );
    textEdit->setTextCursor( cursor );
    

    @
    My guess was to store off the old cursor first, select/change things, and then restore the old cursor back. It seems to work, but I'm no expert.



  • When you get the value of theCursor, you are only getting a copy. You can make the changes to your copy as you do, but then must call setTextCursor() (if I recall correctly), passing in your modified cursor, for it to take effect.

    Peter



  • (edit: see the post just above this one : this is code that does the same)
    @
    textEdit = new QTextEdit(tr("This is sample data"));

    textEdit->selectAll();
    textEdit->setFontPointSize( 20.0 );
    QTextCursor cursor = textEdit->textCursor();
    cursor.movePosition( QTextCursor::End );
    textEdit->setTextCursor( cursor );
    

    @



  • Thank you. Both of your ideas work. There are so many possible combinations of things that seem like they might work, that I never hit on the right one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.