QText编辑。如何手动选择文本?

发布于 2025-01-07 05:52:40 字数 226 浏览 0 评论 0原文

有类似 textEdit->textCursor()->selectionStart()textEdit->textCursor()->selectionEnd() 的函数,但没有函数setSelectionStartsetSelectionEnd

有没有办法手动选择文本的某些部分?

There are functions like textEdit->textCursor()->selectionStart() and textEdit->textCursor()->selectionEnd(), but there are no functions setSelectionStart, setSelectionEnd.

Is there any way to select some part of text manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雪若未夕 2025-01-14 05:52:40
QTextCursor c = textEdit->textCursor();
c.setPosition(startPos);
c.setPosition(endPos, QTextCursor::KeepAnchor);
textEdit->setTextCursor(c);

这段代码使用 将光标移动到选择的起始位置setPosition,然后将其移动到选择的末尾,但通过指定 MoveMode 作为第二个参数。

最后一行将选择设置为在编辑控件内可见,因此如果您只想对所选文本进行一些操作,则应该跳过它。

另外,如果您没有确切的位置,movePosition 很有帮助:您可以在 各种方式,例如向右一个单词或向下一行。

QTextCursor c = textEdit->textCursor();
c.setPosition(startPos);
c.setPosition(endPos, QTextCursor::KeepAnchor);
textEdit->setTextCursor(c);

This piece of code moves the cursor to the start position of the selection using setPosition, then moves it to the end of the selection, but leaves the selection anchor at the old position by specifying a MoveMode as the second parameter.

The last line sets the selection to be visible inside the edit control, so you should skip it if you just want to do some manipulations with the selected text.

Also, if you don't have the exact positions, movePosition is helpful: you can move the cursor in various ways, such as one word to the right or down one line.

゛时过境迁 2025-01-14 05:52:40

我遇到了类似的问题。
在Windows 10中,可能存在“拖动/移动”的错误。我们使用 QT_NO_DRAGANDDROP 作为编译器选项,这使得 QTextEdit 中的文本选择不再起作用。

解决方案:

void QTextEditEx::mouseMoveEvent(QMouseEvent *event)
{
    QTextEdit::mouseMoveEvent(event);
    if (event->buttons() & Qt::LeftButton)
    {
        QTextCursor cursor = textCursor();
        QTextCursor endCursor = cursorForPosition(event->pos()); // key point
        cursor.setPosition(pos, QTextCursor::MoveAnchor);
        cursor.setPosition(endCursor.position(), QTextCursor::KeepAnchor);
        setTextCursor(cursor);
    }
}

void QTextEditEx::mousePressEvent(QMouseEvent *event)
{
    QTextEdit::mousePressEvent(event);
    if (event->buttons() & Qt::LeftButton)
    {
        QTextCursor cursor = cursorForPosition(event->pos());
        // int pos; member variable
        pos = cursor.position();
        cursor.clearSelection();
        setTextCursor(cursor);
    }
}

参考:

  1. 现有的两个答案

  2. QTextEdit:获取单词在鼠标指针下?

I encountered a similar problem.
In Windows 10, there might be a bug of 'drag/move'. We use QT_NO_DRAGANDDROP as a compiler option, which makes text selection in QTextEdit not work anymore.

Solution:

void QTextEditEx::mouseMoveEvent(QMouseEvent *event)
{
    QTextEdit::mouseMoveEvent(event);
    if (event->buttons() & Qt::LeftButton)
    {
        QTextCursor cursor = textCursor();
        QTextCursor endCursor = cursorForPosition(event->pos()); // key point
        cursor.setPosition(pos, QTextCursor::MoveAnchor);
        cursor.setPosition(endCursor.position(), QTextCursor::KeepAnchor);
        setTextCursor(cursor);
    }
}

void QTextEditEx::mousePressEvent(QMouseEvent *event)
{
    QTextEdit::mousePressEvent(event);
    if (event->buttons() & Qt::LeftButton)
    {
        QTextCursor cursor = cursorForPosition(event->pos());
        // int pos; member variable
        pos = cursor.position();
        cursor.clearSelection();
        setTextCursor(cursor);
    }
}

reference:

  1. Two existing answers

  2. QTextEdit: get word under the mouse pointer?

也只是曾经 2025-01-14 05:52:40

尝试使用:

QTextCursor cur = tw->textCursor();
cur.clearSelection();
tw->setTextCursor(cur);

Try to use:

QTextCursor cur = tw->textCursor();
cur.clearSelection();
tw->setTextCursor(cur);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文