如果用户在QLineEdit中键入文本并离开QLineEdit输入字段,我想采取一些措施。
关于focusInEvent
的解释不多。我正在使用Python和PySide2。但是所有信息都在帮助。
def check_focus(self, event):
print('Focus in event')
# Way one I tried
if QtGui.QFocusEvent.lostFocus(self.LE_sample_input_01):
if self.LE_sample_input_01.text():
print("Lost focus")
# Way two I tried
if event.type() == QtCore.QEvent.FocusIn:
if self.LE_sample_input_01.text():
print(f"Lost focus")
# Way three I tried
if self.LE_sample_input_01.focusInEvent(event)
因此可以留下一个条目
from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.QtGui import QIcon
class QTApp(QtWidgets.QWidget):
def __init__(self):
super(QTApp, self).__init__()
self.LE_sample_input_01 = QtWidgets.QLineEdit()
self.LE_sample_input_02 = QtWidgets.QLineEdit()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.LE_sample_input_01)
layout.addWidget(self.LE_sample_input_02)
self.setLayout(layout)
def check_focus(self, event):
print('focus out event')
# if QtGui.QFocusEvent.lostFocus(self.LE_client_name):
# print("lost focus") self.LE_client_name.focusInEvent(event)
if event.type() == QtCore.QEvent.FocusIn:
if self.LE_sample_input_01.text():
print(f"Lost focus")
if __name__ == '__main__':
app = QtWidgets.QApplication()
qt_app = QTApp()
qt_app.show()
app.exec_()
该check_focus方法不存在,因此显然无法使用。如果要侦听来自另一个QObject(例如QWidet)的事件,则应使用eventFilter
:
eventFilter