Results 1 to 3 of 3

Thread: problem while moving QTextCursor

  1. #1
    Join Date
    Mar 2014
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default problem while moving QTextCursor

    i have an action called bold, when i select text then run this action this code will run:

    Qt Code:
    1. void MarkEditWindow::on_actionBold_triggered()
    2. {
    3. QTextCursor selectionBegin(ui->markdownEdit->document());
    4. selectionBegin.setPosition(ui->markdownEdit->textCursor().anchor());
    5. selectionBegin.insertText("**");
    6.  
    7. QTextCursor selectionEnd(ui->markdownEdit->document());
    8. selectionEnd.setPosition(ui->markdownEdit->textCursor().position());
    9. selectionEnd.insertText("**");
    10. }
    To copy to clipboard, switch view to plain text mode 


    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:

    Qt Code:
    1. selectionEnd.movePosition(QTextCursor::Left);
    2. selectionEnd.movePosition(QTextCursor::Left);
    To copy to clipboard, switch view to plain text mode 

    but didn’t work! :-(

  2. #2
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem while moving QTextCursor

    I'm a little confused as to what you are trying to accomplish. Are you trying to add "**" to the start and end of the current selection? You also mention wanting to select the word but also that the first set of ** is not prepended to the text. Are both of these the problems you are asking about?

    Firstly, I'm not sure why you declare 2 different QTextCursors, and that could be related to your problem with the inserted text not showing up, I'm not really sure. As far as a making selection goes you want to use a single QTextCursor. if the cursor's position is already on the word, line, or block you want to select you can use cursor.select(QTextCursor::WordUnderCursor) or any of the other premade selection modes. If you need a more nuanced selection what you need to do is move the cursor (you can use setPosition) to the start of your selection, and then move it to the end but include the MoveMode argument QTextCursor::KeepAnchor. This will keep your anchor at the previous selection and move the cursor so that everything between the 2 positions is selected.

  3. #3
    Join Date
    Mar 2014
    Posts
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem while moving QTextCursor

    i already inserted text before and after the selection.
    one QTextCursor to insert before selection
    and the other to insert after the selection
    i have:
    snapshot121.png
    what i done:
    snapshot122.png
    and what i want:
    snapshot123.png
    not a big problem. but for example when the user choose 2 styles sequentially this will be a problem.

Similar Threads

  1. Problem with moving QWinWidget
    By jazuju in forum Qt Programming
    Replies: 14
    Last Post: 15th September 2011, 14:51
  2. Replies: 0
    Last Post: 24th May 2010, 13:19
  3. Replies: 1
    Last Post: 1st June 2008, 12:04
  4. The drag and moving problem??
    By Ray in forum Qt Programming
    Replies: 10
    Last Post: 28th September 2006, 10:57
  5. a annoying problem about QTextCursor
    By kaos33 in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2006, 06:00

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.