View Full Version : problem while moving QTextCursor

29th October 2014, 17:28
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());

QTextCursor selectionEnd(ui->markdownEdit->document());

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:


but didn’t work! :-(

31st October 2014, 20:27
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.

1st November 2014, 12:45
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:
what i done:
and what i want:
not a big problem. but for example when the user choose 2 styles sequentially this will be a problem.