python - 当我在 pyqt5 中按 Enter 时转到下一个 LineEdit
问题描述
我希望当用户在键盘光标上按 Enter 时自动转到下一行编辑。与 TabOrder 类似,但带有 Enter。
有人有建议吗?
解决方案
一种可能的解决方案是拦截 KeyPress 事件并验证是否按下了Enter键然后调用该focusNextPrevChild()
方法:
from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import (
QApplication,
QComboBox,
QLineEdit,
QPushButton,
QVBoxLayout,
QWidget,
)
class Widget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
lay = QVBoxLayout(self)
lay.addWidget(QLineEdit())
lay.addWidget(QPushButton())
lay.addWidget(QLineEdit())
lay.addWidget(QComboBox())
def event(self, event):
if event.type() == QEvent.KeyPress and event.key() in (
Qt.Key_Enter,
Qt.Key_Return,
):
self.focusNextPrevChild(True)
return super().event(event)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec())
推荐阅读
- r - 从 r 中的数据帧中抽取样本,与二进制结果成比例
- python - Airflow:如何将 PyMySQL 与 MySQL Hook 一起使用?
- twitter-bootstrap - 使用 Bootstrap 4 和 Flexbox 定位导航栏按钮不起作用?
- java - Admob 既不展示测试广告,也不展示真实广告
- scala - 多个 SBT 配置应该是独占的,但它们都同时激活 - 为什么?
- excel - 根据月份在汇总表中填充数据的公式
- mysql - 无法在 MySQL 查询中找到最大平均值
- google-bigquery - 带有 BigQuery 的 Pyodbc
- android - 删除内部文件
- python - 如何使用opencv和python去除图像上的水平线和垂直线