PDA

View Full Version : problem while moving QTextCursor



Abdeljalil
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());
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! :-(

forgottenduck
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.

Abdeljalil
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:
10719
what i done:
10720
and what i want:
10721
not a big problem. but for example when the user choose 2 styles sequentially this will be a problem.