单击窗口上的任何其他位置时如何从 QLineEdit 中移除焦点
我正在开发一个自定义 Qt 按钮,如果双击它,您可以编辑按钮上的文本。双击按钮时,会在按钮上的文本处出现 QLineEdit
,允许用户编辑按钮上的文本。我的要求是,如果用户单击应用程序窗口中的任意位置,QLineEdit
应消失并取消编辑操作。这在某些情况下有效。具体来说,如果我单击任何能够输入文本的内容,它就会起作用。窗口的其他部分无法按预期工作。我将单击应用程序窗口的空白部分,QLineEdit
将保留其焦点。在这些情况下我怎样才能消除它的焦点?
I'm working on a custom Qt button that allows you to edit the text on the button if you double click it. When the button is double clicked, a QLineEdit
appears where the text on the button is allowing the user to edit the text on the button. My requirement is that if the user clicks anywhere in the application window, the QLineEdit
should disappear and cancel the edit operation. This works in some cases. Specifically, it works if I click on anything that is capable of text entry. Other portions of the window don't work as expected. I'll click on a blank portion of the application window, and the QLineEdit
retains its focus. How can I remove its focus in these cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找到了一个似乎有效的解决方案,尽管我仍然愿意接受其他选择(如果有)。我使用的是 PyQt4,所以我的示例是 python:
创建 QLineEdit 的子类,这样我就有了一个新类型。我不希望或不需要在所有 QLineEdit 实例上出现这种行为;只是这些具体的。
现在,在我的 QMainWindow 子类中,我重写了 mousePressEvent() 实现。它获取当前聚焦的小部件。如果该小部件的类型为
MyLineEdit
,请清除焦点。这让我得到了我正在寻找的行为,这样如果用户单击应用程序窗口上的任意位置,焦点就会被清除。
编辑:我确实发现了一个警告。我的主窗口中有一个 QTreeView 。如果用户单击树视图,焦点不会从文本编辑字段中移除。
I've found a solution that seems to work, though I'm still open to other options if there are any. I'm using PyQt4, so my example is in python:
Create a subclass of QLineEdit just so I have a new type. I don't want or need this behavior on all QLineEdit instances; just these specific ones.
Now, in my QMainWindow subclass, I override the
mousePressEvent()
implementation. It gets the currently focused widget. If that widget is of typeMyLineEdit
, clear the focus.This gets me the behavior I'm looking for so that if the user clicks anywhere on the application's window, the focus is cleared.
Edit: I did find one caveat to this. I have a
QTreeView
in the main window. If the user clicks on the tree view, focus is not removed from the text edit field.捕获父窗口小部件的 clicked() 信号并调用 yourLabel->clearFocus() (不幸的是,这不是一个槽,使事情变得更加复杂)。
Catch the clicked() signal of your parent widget and call yourLabel->clearFocus() (that unfortunatelly happens to not be a slot, making things more complicated) there.
我遵循了 Grant Limberg 的指示,但发现就我而言,一个简单的方法:
就可以解决问题。
I followed Grant Limberg instruction here but figured out that, in my case, a simple:
would fix the problem.
我不确定这是否也适用于 Qt4(我使用的是 PyQt5),但您可以更改
QMainWindow
或parent widget
的FocusPolicy
> 清除QLineEdit
中的焦点。根据 https://doc.qt.io/qt-5/qwidget .html#focusPolicy-prop我已将
QMainWindow
的策略更改为Qt.StrongFocus
,它的工作方式与问题中描述的功能类似。I'm not sure if this also works in Qt4 (I'm using PyQt5) but you can change the
FocusPolicy
of theQMainWindow
orparent widget
to clear the focus in theQLineEdit
. As per https://doc.qt.io/qt-5/qwidget.html#focusPolicy-propI've changed the policy of my
QMainWindow
toQt.StrongFocus
and it worked like the functionality described in the question.如果在 C++ 中完成,我会做一些类似的事情:
对于这种特殊情况,我可能会使用
editingFinished()
而不是returnPressed()
,但我不会使用textChanged(QString)
。If done in C++ I would do something along the lines of:
For this particular case I would use
editingFinished()
instead ofreturnPressed()
, probably, but I would NOT usetextChanged(QString)
.