问题描述
我正在使用QT v5.2
I am working on QT v5.2
我需要隐藏 QLineEdit
永久。
但同时,我希望 QLineEdit
是可编辑的(因此对我来说,readOnly和/或将editable设置为false不是可选项)。
I need to hide the blinking cursor (caret) of QLineEdit
permanently.But at the same time, I want the QLineEdit
to be editable (so readOnly and/or setting editable false is not an option for me).
当焦点对准时,我已经在更改 QLineEdit
的背景颜色,所以我会知道哪个 QLineEdit
小部件正在编辑。
对于我的要求,光标(闪烁的文本光标)显示不应存在。
I am already changing the Background color of the QLineEdit
when it is in focus, so I will know which QLineEdit
widget is getting edited.For my requirement, cursor (the blinking text cursor) display should not be there.
我尝试了 styleSheets
,但我无法隐藏光标({color:transparent; text-shadow:0px 0px 0px black;})
I have tried styleSheets
, but I can't get the cursor hidden ( {color:transparent; text-shadow:0px 0px 0px black;} )
有人可以让我知道如何实现吗?
Can someone please let me know how can I achieve this?
推荐答案
没有标准方法为此,但是您可以使用 setReadOnly
方法隐藏光标。调用此方法时,它会禁用键的处理,因此您需要强制执行此操作。
从QLineEdit继承并重新实现 keyPressEvent
。
There is no standard way to do that, but you can use setReadOnly
method which hides the cursor. When you call this method it disables processing of keys so you'll need to force it.Inherit from QLineEdit and reimplement keyPressEvent
.
LineEdit::LineEdit(QWidget* parent)
: QLineEdit(parent)
{
setReadOnly(true);
}
void LineEdit::keyPressEvent(QKeyEvent* e)
{
setReadOnly(false);
__super::keyPressEvent(e);
setReadOnly(true);
}
这篇关于隐藏QLineEdit闪烁的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!