pyqt5 - QWizardPage 中的 QSpinbox 获得初始焦点,即使其焦点策略设置为 NoFocus

标签 pyqt5 focus qspinbox qwizard qwizardpage

我在 QWizardPage 中创建了一个 QSpinBox。即使我将 focusPolicy 设置为 Qt.Nofocus,它也会在向导页面的开头获得焦点。

我在Qt Creator中设置的是:

enter image description here

但是当向导页面启动时,我得到:

enter image description here

然后,如果我多次按 Tab,焦点将按预期在 FinishCancel 之间移动。

我在 Windows 10 上使用 PyQt 5.15.6。我的 python 代码由 pyuic5 生成的 WizardPage.py 文件和 test_wizard.py 文件组成。

WizardPage.py 文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'wizardpage.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_WizardPage(object):
    def setupUi(self, WizardPage):
        WizardPage.setObjectName("WizardPage")
        WizardPage.resize(400, 300)
        self.spinBox = QtWidgets.QSpinBox(WizardPage)
        self.spinBox.setGeometry(QtCore.QRect(40, 50, 42, 22))
        self.spinBox.setFocusPolicy(QtCore.Qt.NoFocus)
        self.spinBox.setObjectName("spinBox")

        self.retranslateUi(WizardPage)
        QtCore.QMetaObject.connectSlotsByName(WizardPage)

    def retranslateUi(self, WizardPage):
        _translate = QtCore.QCoreApplication.translate
        WizardPage.setWindowTitle(_translate("WizardPage", "WizardPage"))

test_wizard.py 文件:

from PyQt5 import QtCore, QtGui, QtWidgets
from WizardPage import *
from PyQt5.Qt import *
from PyQt5.QtCore import *
import sys


class test_wizard_page(QtWidgets.QWizardPage, Ui_WizardPage):
    def __init__(self, parent):
        super().__init__(parent)
        self.setupUi(self)


class MyWizard(QtWidgets.QWizard):
    def __init__(self):
        super().__init__()

        self.test_wizard_page = test_wizard_page(self)
        # self.test_wizard_page.setE
        self.addPage(self.test_wizard_page)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    wizard = MyWizard()
    wizard.show()
    sys.exit(app.exec_())

我使用 Qt Creator 创建的 .ui 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>WizardPage</class>
 <widget class="QWizardPage" name="WizardPage">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>WizardPage</string>
  </property>
  <widget class="QSpinBox" name="spinBox">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>50</y>
     <width>42</width>
     <height>22</height>
    </rect>
   </property>
   <property name="focusPolicy">
    <enum>Qt::NoFocus</enum>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

This question可能与我的问题有关。我也尝试重现他们的问题,但我当前使用的 PyQt 失败了。我的问题也是Qt的bug吗?如何处理这个问题?

最佳答案

我可以重现该行为,并且它仍然存在于 PyQt6 中。解决方法是在旋转框的行编辑上设置焦点策略(也不必在旋转框上设置它)。如果您还想恢复正常的对焦行为,可以使用单次计时器来完成:

class test_wizard_page(QtWidgets.QWizardPage, Ui_WizardPage):
    def __init__(self, parent):
        super().__init__(parent)
        self.setupUi(self)
        for spinbox in self.findChildren(QtWidgets.QSpinBox):
            edit = spinbox.lineEdit()
            policy = edit.focusPolicy()
            edit.setFocusPolicy(QtCore.Qt.NoFocus)
            QtCore.QTimer.singleShot(
                0, lambda edit=edit, policy=policy:
                    edit.setFocusPolicy(policy))

关于pyqt5 - QWizardPage 中的 QSpinbox 获得初始焦点,即使其焦点策略设置为 NoFocus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72353937/

相关文章:

python - 如何在pyQt5中接收ActiveX事件?

python - PyQT 删除 QVBoxLayout 以适应数据库中的更新值

python - Pyqt 两个按钮连接到在标签中设置文本的方法

Javascript焦点删除文本突出显示

qt - 在 QSpinBox 中设置特定值

python-3.x - PyQt5 中的多线程

JavaScript 焦点不工作

javascript - 如何模糊 iframe?

qt - 如何在 QML 旋转框中使用 float

c++ - 将字符串设置为 QDoubleSpinBox