不在编辑模式时如何隐藏插入符号? [英] How can I hide the caret when not in edit mode?

查看:41
本文介绍了不在编辑模式时如何隐藏插入符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextItem 继承了 QGraphicsTextItem.我这样做是为了在双击时我可以编辑文本,当单击时,文本不再可编辑.

I have a TextItem inheriting QGraphicsTextItem. I made it so that on double-click I can edit the text, and when clicking out, the text is no longer editable.

void TextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
{
    setTextInteractionFlags(Qt::TextEditorInteraction);
    setFocus();

    int p = document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit);
    QTextCursor _cursor = textCursor();
    _cursor.setPosition(p);
    setTextCursor(_cursor);
}

void TextItem::focusOutEvent(QFocusEvent *event)
{
    Q_UNUSED(event);
    setTextInteractionFlags(Qt::NoTextInteraction);
}

点击后,文本不再可编辑 - 但插入符号仍然可见.

When clicking out, the text is no longer editable - but the caret is still visible.

focusOutEvent 中添加 setCursor(Qt::OpenHandCursor);(并且可能试图记住要设置的光标形状......我还不知道如何) 解决这个问题 - 使插入符号消失 - 但我认为这不是正确的解决方法.

Adding setCursor(Qt::OpenHandCursor); in the focusOutEvent (and possibly trying to remember which cursor shape to set... I don't know how yet) fixes this - makes the caret disappear - but I don't think it is the right fix.

然而,当不再处于编辑模式时,我在 QTextCursor 中找不到任何隐藏插入符号的方法 - 似乎设置 NoTextInteraction 应该做到这一点...

Yet I cannot find any method in QTextCursor to hide the caret when no longer in edit mode - and it seems that setting NoTextInteraction should have done that...

不处于编辑模式时隐藏插入符号的最佳方法是什么?

What is the best way to hide the caret when not in edit mode ?

推荐答案

在失去焦点时添加了一个明确的选择,因为似乎不仅插入符号被留下,而且任何选定的文本也被左选中 - 而这不是行为我想要.

Added a clear selection on lost focus, since it seems that not only the caret is left but any selected text is also left selected - and it is not the behavior I wanted.

void TextItem::focusOutEvent(QFocusEvent *event)
{
    Q_UNUSED(event);
    setTextInteractionFlags(Qt::NoTextInteraction);

    QTextCursor _cursor = textCursor();
    _cursor.clearSelection();
    setTextCursor(_cursor);
}

以上清除插入符号以及任何选定的文本片段.

The above clears the caret, as well as any selected text fragment.

(如果阅读此问题的任何人都希望保存对文本片段的选择,但不显示插入符号,则我在问题中提到的选项 - 通过设置 QCursor - 可能是最佳选择)

(If anybody reading this question is looking to save selection on text fragments, but not display the caret, the option I mentioned in my question - by setting the QCursor - may be the best option)

这篇关于不在编辑模式时如何隐藏插入符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆