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. QTextEdit.selectAll() and clearSelection().
Forum Updated to NodeBB v4.3 + New Features

QTextEdit.selectAll() and clearSelection().

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 10.4k 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.
  • R Offline
    R Offline
    RSFJ
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gimble
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        donjuedo
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gimble
          wrote on last edited by
          #4

          (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 );
          

          @

          1 Reply Last reply
          1
          • R Offline
            R Offline
            RSFJ
            wrote on last edited by
            #5

            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.

            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