gpt4 book ai didi

python - 防止按下 Tab 键时特定的 QLineEdit 聚焦

转载 作者:行者123 更新时间:2023-12-01 07:51:25 27 4
gpt4 key购买 nike

我有一个 pyqt5 GUI。我想使用 Tab 键在 GUI 中导航。但我想防止特定的 QLineEdit 小部件在按下选项卡后获得焦点。我怎样才能做到这一点?

最佳答案

您可以设置所有控件都继承自的 QWidget 类中可用的属性 focusPolicy,例如:

import sys
from PyQt5.Qt import QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator, QDoubleValidator
from PyQt5.QtWidgets import QWidget, QLineEdit, QFormLayout


class MainWindow(QWidget):
def __init__(self):
super().__init__()

self.e1 = QLineEdit()
self.e1.setValidator(QIntValidator())
self.e1.setMaxLength(4)
self.e1.setAlignment(Qt.AlignRight)

# This control will focus using the tab key
self.e1.setFocusPolicy(Qt.ClickFocus | Qt.TabFocus)

e2 = QLineEdit()
e2.setValidator(QDoubleValidator(0.99, 99.99, 2))

# This control will not focus using the tab key
e2.setFocusPolicy(Qt.ClickFocus | Qt.NoFocus)

self.flo = QFormLayout()
self.flo.addRow("integer validator", self.e1)
self.flo.addRow("Double validator", e2)

e3 = QLineEdit()
e3.setInputMask('+99_9999_999999')
self.flo.addRow("Input Mask", e3)

# This control will not focus using the tab key
e3.setFocusPolicy(Qt.ClickFocus | Qt.NoFocus)

e4 = QLineEdit()
self.flo.addRow("Text changed", e4)

# This control will focus using the tab key (this is the default behaviour you don't need to explicitly set it)
e4.setFocusPolicy(Qt.ClickFocus | Qt.TabFocus)

# Default focus behaviour
e5 = QLineEdit()
e5.setEchoMode(QLineEdit.Password)
self.flo.addRow("Password", e5)

e6 = QLineEdit("Hello Python")
e6.setReadOnly(True)
self.flo.addRow("Read Only", e6)

self.setLayout(self.flo)
self.setWindowTitle("Example")


if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())

更多信息请点击:https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop

关于python - 防止按下 Tab 键时特定的 QLineEdit 聚焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56197335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com