如何知道 ui 表单中的哪个 qwidget 在 pyqt 中获得了焦点
How to know which qwidget in a ui form got the focus in pyqt
我是 PyQt 新手。我在 QtDeveloper 中设计了一个具有三个控件的窗体。一个按钮,一个组合框和一个行编辑。我的 ui 表单中的行编辑小部件的名称是 myLineEdit。我想知道哪个 Qwidget 获得了焦点(QLineEdit 或 QComboBox)。我实现了从互联网上获得的代码。当代码 运行 时,创建了一个单独的行编辑并且它工作正常。但我想将 focusInEvent 提供给以 .ui 形式创建的 myLineEdit 小部件。给出了我的代码。请帮忙
class MyLineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(MyLineEdit, self).__init__(parent)
def focusInEvent(self, event):
print 'focus in event'
self.clear()
QLineEdit.focusInEvent(self, QFocusEvent(QEvent.FocusIn))
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.myLineEdit = MyLineEdit(self)
您必须实施 eventFilter
方法并将此 属性 启用到需要的小部件:
{your widget}.installEventFilter(self)
eventFilter 方法具有事件对象和类型的信息。
例子
import sys
from PyQt5 import uic
from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QApplication, QWidget
uiFile = "widget.ui" # Enter file here.
Ui_Widget, _ = uic.loadUiType(uiFile)
class Widget(QWidget, Ui_Widget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.setupUi(self)
self.lineEdit.installEventFilter(self)
self.pushButton.installEventFilter(self)
self.comboBox.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.FocusIn:
if obj == self.lineEdit:
print("lineedit")
elif obj == self.pushButton:
print("pushbutton")
elif obj == self.comboBox:
print("combobox")
return super(Widget, self).eventFilter(obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
输出:
lineedit
pushbutton
combobox
pushbutton
lineedit
我是 PyQt 新手。我在 QtDeveloper 中设计了一个具有三个控件的窗体。一个按钮,一个组合框和一个行编辑。我的 ui 表单中的行编辑小部件的名称是 myLineEdit。我想知道哪个 Qwidget 获得了焦点(QLineEdit 或 QComboBox)。我实现了从互联网上获得的代码。当代码 运行 时,创建了一个单独的行编辑并且它工作正常。但我想将 focusInEvent 提供给以 .ui 形式创建的 myLineEdit 小部件。给出了我的代码。请帮忙
class MyLineEdit(QtGui.QLineEdit):
def __init__(self, parent=None):
super(MyLineEdit, self).__init__(parent)
def focusInEvent(self, event):
print 'focus in event'
self.clear()
QLineEdit.focusInEvent(self, QFocusEvent(QEvent.FocusIn))
class MainWindow(QtGui.QMainWindow,Ui_MainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.myLineEdit = MyLineEdit(self)
您必须实施 eventFilter
方法并将此 属性 启用到需要的小部件:
{your widget}.installEventFilter(self)
eventFilter 方法具有事件对象和类型的信息。
例子
import sys
from PyQt5 import uic
from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QApplication, QWidget
uiFile = "widget.ui" # Enter file here.
Ui_Widget, _ = uic.loadUiType(uiFile)
class Widget(QWidget, Ui_Widget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.setupUi(self)
self.lineEdit.installEventFilter(self)
self.pushButton.installEventFilter(self)
self.comboBox.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.FocusIn:
if obj == self.lineEdit:
print("lineedit")
elif obj == self.pushButton:
print("pushbutton")
elif obj == self.comboBox:
print("combobox")
return super(Widget, self).eventFilter(obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
输出:
lineedit
pushbutton
combobox
pushbutton
lineedit