单击窗口上的任何其他位置时如何从 QLineEdit 中移除焦点

发布于 2024-10-07 16:46:10 字数 273 浏览 0 评论 0原文

我正在开发一个自定义 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 技术交流群。

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

发布评论

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

评论(5

随遇而安 2024-10-14 16:46:10

我找到了一个似乎有效的解决方案,尽管我仍然愿意接受其他选择(如果有)。我使用的是 PyQt4,所以我的示例是 python:

创建 QLineEdit 的子类,这样我就有了一个新类型。我不希望或不需要在所有 QLineEdit 实例上出现这种行为;只是这些具体的。

class MyLineEdit(QtGui.QLineEdit):
    pass

现在,在我的 QMainWindow 子类中,我重写了 mousePressEvent() 实现。它获取当前聚焦的小部件。如果该小部件的类型为 MyLineEdit,请清除焦点。

class MyMainWindow(QtGui.QMainWindow):
    def ...

    def mousePressEvent(self, event):
        focused_widget = QtGui.QApplication.focusWidget()
        if isinstance(focused_widget, MyLineEdit):
            focused_widget.clearFocus()
        QtGui.QMainWindow.mousePressEvent(self, event)

    def ...

这让我得到了我正在寻找的行为,这样如果用户单击应用程序窗口上的任意位置,焦点就会被清除。


编辑:我确实发现了一个警告。我的主窗口中有一个 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.

class MyLineEdit(QtGui.QLineEdit):
    pass

Now, in my QMainWindow subclass, I override the mousePressEvent() implementation. It gets the currently focused widget. If that widget is of type MyLineEdit, clear the focus.

class MyMainWindow(QtGui.QMainWindow):
    def ...

    def mousePressEvent(self, event):
        focused_widget = QtGui.QApplication.focusWidget()
        if isinstance(focused_widget, MyLineEdit):
            focused_widget.clearFocus()
        QtGui.QMainWindow.mousePressEvent(self, event)

    def ...

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.

安静被遗忘 2024-10-14 16:46:10

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.

此刻的回忆 2024-10-14 16:46:10

我遵循了 Grant Limberg 的指示,但发现就我而言,一个简单的方法:

QApplication.focusWidget().clearFocus()

就可以解决问题。

I followed Grant Limberg instruction here but figured out that, in my case, a simple:

QApplication.focusWidget().clearFocus()

would fix the problem.

ま柒月 2024-10-14 16:46:10

我不确定这是否也适用于 Qt4(我使用的是 PyQt5),但您可以更改 QMainWindowparent widgetFocusPolicy > 清除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 the QMainWindow or parent widget to clear the focus in the QLineEdit. As per https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

I've changed the policy of my QMainWindow to Qt.StrongFocus and it worked like the functionality described in the question.

清醇 2024-10-14 16:46:10

如果在 C++ 中完成,我会做一些类似的事情:

connect(myWidgets->MyLineEdit, SIGNAL(returnPressed()), this, SLOT(onLineEditDone());

void onLineEditDone()
{
    myWidgets->MyLineEdit->clearFocus();
}

对于这种特殊情况,我可能会使用 editingFinished() 而不是 returnPressed(),但我不会使用textChanged(QString)

If done in C++ I would do something along the lines of:

connect(myWidgets->MyLineEdit, SIGNAL(returnPressed()), this, SLOT(onLineEditDone());

void onLineEditDone()
{
    myWidgets->MyLineEdit->clearFocus();
}

For this particular case I would use editingFinished() instead of returnPressed(), probably, but I would NOT use textChanged(QString).

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