相关疑难解决方法(0)

将光标移动到QTextEdit内部

我有一个表格,上面有一个QTextEdit叫做的表格translationInput.我正在尝试为用户提供编辑功能.

QTextEdit将包含HTML格式的文本.我有一组按钮,如" 粗体 "," 斜体 "等,它们应该将相应的标签添加到文档中.如果在没有选择文本时按下按钮,我只想插入一对标签,例如<b></b>.如果选择了某些文本,我希望标签从中向左和向右显示.

这很好用.但是,我还希望在此之后将光标放在结束标记之前,这样用户就可以继续在新添加的标记内输入,而无需手动重新定位光标.默认情况下,光标出现新添加的文本之后(所以在我的情况下,就在结束标记之后).

这是我对Italic按钮的代码:

//getting the selected text(if any), and adding tags.
QString newText = ui.translationInput->textCursor().selectedText().prepend("<i>").append("</i>");
//Inserting the new-formed text into the edit
ui.translationInput->insertPlainText( newText );
//Returning focus to the edit
ui.translationInput->setFocus();
//!!! Here I want to move the cursor 4 characters left to place it before the </i> tag.
ui.translationInput->textCursor().movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 4);
Run Code Online (Sandbox Code Playgroud)

但是,最后一行没有做任何事情,即使movePosition()返回true,光标也不会移动,这意味着所有操作都成功完成.

我也尝试过这样做QTextCursor::PreviousCharacter而不是QTextCursor::Left …

c++ qt qtextedit qtextcursor

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

qt ×1

qtextcursor ×1

qtextedit ×1