pyqt5 - 小部件焦点不按照 setTabOrder 运行

发布于 2025-01-11 14:47:26 字数 4147 浏览 0 评论 0原文

我有一个简单的问题,但我无法摆脱它:我已经在表单中设置了小部件的选项卡顺序,但是,尽管代码中的选项卡顺序是正确的,但当显示表单时,不遵守这种顺序。具体来说,在小部件“self.dateEdit”按 Tab 键之后,焦点不是将焦点移至“self.comboBox”小部件,而是转到“self.doubleSpinBox”小部件。下面是代码 知道为什么吗?谢谢

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(474, 433)
    self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
    self.verticalLayout.setObjectName("verticalLayout")
    self.formLayout = QtWidgets.QFormLayout()
    self.formLayout.setObjectName("formLayout")
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setObjectName("label")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
    self.lineEdit = QtWidgets.QLineEdit(Dialog)
    self.lineEdit.setObjectName("lineEdit")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
    self.label_2 = QtWidgets.QLabel(Dialog)
    self.label_2.setObjectName("label_2")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
    self.dateEdit = QtWidgets.QDateEdit(Dialog)
    self.dateEdit.setObjectName("dateEdit")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.dateEdit)
    self.label_3 = QtWidgets.QLabel(Dialog)
    self.label_3.setObjectName("label_3")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
    self.comboBox = QtWidgets.QComboBox(Dialog)
    self.comboBox.setObjectName("comboBox")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.comboBox)
    self.label_4 = QtWidgets.QLabel(Dialog)
    self.label_4.setObjectName("label_4")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)
    self.comboBox_2 = QtWidgets.QComboBox(Dialog)
    self.comboBox_2.setObjectName("comboBox_2")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.comboBox_2)
    self.label_5 = QtWidgets.QLabel(Dialog)
    self.label_5.setObjectName("label_5")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_5)
    self.doubleSpinBox = QtWidgets.QDoubleSpinBox(Dialog)
    self.doubleSpinBox.setObjectName("doubleSpinBox")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.doubleSpinBox)
    self.label_6 = QtWidgets.QLabel(Dialog)
    self.label_6.setObjectName("label_6")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_6)
    self.textEdit = QtWidgets.QTextEdit(Dialog)
    self.textEdit.setObjectName("textEdit")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.textEdit)
    self.verticalLayout.addLayout(self.formLayout)
    self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
    self.buttonBox.setCenterButtons(False)
    self.buttonBox.setObjectName("buttonBox")
    self.verticalLayout.addWidget(self.buttonBox)
    self.label.setBuddy(self.lineEdit)
    self.label_5.setBuddy(self.doubleSpinBox)

    self.retranslateUi(Dialog)
    self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
    self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
    QtCore.QMetaObject.connectSlotsByName(Dialog)
    Dialog.setTabOrder(self.lineEdit, self.dateEdit)
    Dialog.setTabOrder(self.dateEdit, self.comboBox)
    Dialog.setTabOrder(self.comboBox, self.comboBox_2)
    Dialog.setTabOrder(self.comboBox_2, self.doubleSpinBox)
    Dialog.setTabOrder(self.doubleSpinBox, self.textEdit)

def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.label.setText(_translate("Dialog", "Employee &Name"))
    self.label_2.setText(_translate("Dialog", "Employee Date"))
    self.label_3.setText(_translate("Dialog", "Department"))
    self.label_4.setText(_translate("Dialog", "Position"))
    self.label_5.setText(_translate("Dialog", "Annual &Salary"))
    self.label_6.setText(_translate("Dialog", "Job Description"))

I have a simple issue but I can't get out of it: I have set the tab order for the widgets within a form but, despite the tab order in the code is correct, when the form is displayed such order is not respected. Specifically after the widget 'self.dateEdit' pressing tab key, rather than moving the focus to 'self.comboBox' widget it goes to 'self.doubleSpinBox' widget. Below is the code
Any idea why ? Thanks

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(474, 433)
    self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
    self.verticalLayout.setObjectName("verticalLayout")
    self.formLayout = QtWidgets.QFormLayout()
    self.formLayout.setObjectName("formLayout")
    self.label = QtWidgets.QLabel(Dialog)
    self.label.setObjectName("label")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
    self.lineEdit = QtWidgets.QLineEdit(Dialog)
    self.lineEdit.setObjectName("lineEdit")
    self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
    self.label_2 = QtWidgets.QLabel(Dialog)
    self.label_2.setObjectName("label_2")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
    self.dateEdit = QtWidgets.QDateEdit(Dialog)
    self.dateEdit.setObjectName("dateEdit")
    self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.dateEdit)
    self.label_3 = QtWidgets.QLabel(Dialog)
    self.label_3.setObjectName("label_3")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)
    self.comboBox = QtWidgets.QComboBox(Dialog)
    self.comboBox.setObjectName("comboBox")
    self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.comboBox)
    self.label_4 = QtWidgets.QLabel(Dialog)
    self.label_4.setObjectName("label_4")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)
    self.comboBox_2 = QtWidgets.QComboBox(Dialog)
    self.comboBox_2.setObjectName("comboBox_2")
    self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.comboBox_2)
    self.label_5 = QtWidgets.QLabel(Dialog)
    self.label_5.setObjectName("label_5")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_5)
    self.doubleSpinBox = QtWidgets.QDoubleSpinBox(Dialog)
    self.doubleSpinBox.setObjectName("doubleSpinBox")
    self.formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.doubleSpinBox)
    self.label_6 = QtWidgets.QLabel(Dialog)
    self.label_6.setObjectName("label_6")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.label_6)
    self.textEdit = QtWidgets.QTextEdit(Dialog)
    self.textEdit.setObjectName("textEdit")
    self.formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.textEdit)
    self.verticalLayout.addLayout(self.formLayout)
    self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
    self.buttonBox.setCenterButtons(False)
    self.buttonBox.setObjectName("buttonBox")
    self.verticalLayout.addWidget(self.buttonBox)
    self.label.setBuddy(self.lineEdit)
    self.label_5.setBuddy(self.doubleSpinBox)

    self.retranslateUi(Dialog)
    self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
    self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
    QtCore.QMetaObject.connectSlotsByName(Dialog)
    Dialog.setTabOrder(self.lineEdit, self.dateEdit)
    Dialog.setTabOrder(self.dateEdit, self.comboBox)
    Dialog.setTabOrder(self.comboBox, self.comboBox_2)
    Dialog.setTabOrder(self.comboBox_2, self.doubleSpinBox)
    Dialog.setTabOrder(self.doubleSpinBox, self.textEdit)

def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.label.setText(_translate("Dialog", "Employee &Name"))
    self.label_2.setText(_translate("Dialog", "Employee Date"))
    self.label_3.setText(_translate("Dialog", "Department"))
    self.label_4.setText(_translate("Dialog", "Position"))
    self.label_5.setText(_translate("Dialog", "Annual &Salary"))
    self.label_6.setText(_translate("Dialog", "Job Description"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文