QWizardPage 中的 QSpinbox 获得初始焦点,即使其焦点策略设置为 NoFocus
QSpinbox in QWizardPage gets initial focus even if its focus-policy is set to NoFocus
我在 QWizardPage 中创建了一个 QSpinBox。即使我将 focusPolicy 设置为 Qt.Nofocus.
,它也会在向导页面的开头获得焦点
我在Qt creator中设置的是:
但是当向导页面启动时,我得到:
然后,如果我按几次Tab
,焦点将按预期在Finish
和Cancel
之间移动。
我在 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的错误吗?又该如何处理?
我可以重现该行为,它仍然存在于 PyQt6 中。一个work-around是在spin-box的line-edit上设置focus-policy(没必要在spin-box上也设置)。如果您还想恢复正常的焦点行为,可以使用 single-shot 计时器来完成:
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))
我在 QWizardPage 中创建了一个 QSpinBox。即使我将 focusPolicy 设置为 Qt.Nofocus.
,它也会在向导页面的开头获得焦点我在Qt creator中设置的是:
但是当向导页面启动时,我得到:
然后,如果我按几次Tab
,焦点将按预期在Finish
和Cancel
之间移动。
我在 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的错误吗?又该如何处理?
我可以重现该行为,它仍然存在于 PyQt6 中。一个work-around是在spin-box的line-edit上设置focus-policy(没必要在spin-box上也设置)。如果您还想恢复正常的焦点行为,可以使用 single-shot 计时器来完成:
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))