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?
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- QT Layouts, how to make widgets in horizontal layo
- QT GUI freezes even though Im running in separate
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- How to set the font size of the label on pushbutto
- Why doesn't valgrind detect a memory leak in m
- QTreeView remove decoration/expand button for all
- qt界面拥挤
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.
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)
.I followed Grant Limberg instruction here but figured out that, in my case, a simple:
would fix the problem.
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.