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. Problem while moving QTextCursor
Qt 6.11 is out! See what's new in the release blog

Problem while moving QTextCursor

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k 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.
  • A Offline
    A Offline
    Abdeljalil
    wrote on last edited by
    #1

    i have an action called bold, when i select text then run this action this code will run:
    @
    void MarkEditWindow::on_actionBold_triggered()
    {
    QTextCursor selectionBegin(ui->markdownEdit->document());
    selectionBegin.setPosition(ui->markdownEdit->textCursor().anchor());
    selectionBegin.insertText("**");

    QTextCursor selectionEnd(ui->markdownEdit->document());
    selectionEnd.setPosition(ui->markdownEdit->textCursor().position());
    selectionEnd.insertText("**");
    

    }
    @

    now for example i wrote "test" then run the action the result is "test" and the selected text will be "test**" (missing first '**')
    how can i move the cursor after selection to make it select "test" or even select "test" only
    i tried a lot of things and didn't work, like:
    @
    selectionEnd.movePosition(QTextCursor::Left);
    selectionEnd.movePosition(QTextCursor::Left);
    @
    but didn't work! :-(

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you thinking of something like

      @
      QTextCursor cursor = _textEdit->textCursor();
      int start = cursor.selectionStart();
      int end = cursor.selectionEnd();
      cursor.setPosition(start);
      cursor.insertText("");
      cursor.setPosition(end+2);
      cursor.insertText("
      ");
      @

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Abdeljalil
        wrote on last edited by
        #3

        yeah, but:
        @
        cursor.setPosition(end-2);
        @
        how to do that?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @
          // Go back to start and redo the selection
          cursor.setPosition(start + 2);
          cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, (end - start));
          _textEdit->setTextCursor(cursor);
          @

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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