我正在QWizard
中创建一个向导
我有QLineEdit
和QPushButton
# Enter token
self.enter_token_box = QLineEdit()
# Enter token button
self.btn = QPushButton('OK')
# connect button to function, checks the token..
self.btn.clicked.connect(self._EnterToken)
我已经在这一行中接受了回车键,并运行与单击“确定”按钮相同的功能。
# Enter key press connection
self.enter_token_box.returnPressed.connect(self._EnterToken)
问题在于它会同时触发向导的OK
按钮和Next
按钮。
MVCE:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Wizard(QWizard):
def __init__(self, parent=None):
super(Wizard, self).__init__(parent)
self.addPage(EnterToken(self))
self.addPage(ProcessData(self))
class EnterToken(QWizardPage):
def __init__(self, parent=None):
super(EnterToken, self).__init__(parent)
self.setTitle("Enter your token here")
self.setSubTitle(" ")
# Enter Token Widgets
self.label = QLabel()
self.enter_token_box = QLineEdit()
self.btn = QPushButton('OK')
# layout options
layout = QVBoxLayout()
layout.addWidget(self.label)
self.label.setText("Enter Your 12 Digit Code.")
layout.addWidget(self.enter_token_box)
layout.addWidget(self.btn)
# Enter Key TRigger
self.enter_token_box.returnPressed.connect(self._EnterToken)
self.btn.clicked.connect(self._EnterToken)
self.setLayout(layout)
def _EnterToken(self):
""" Method for processing user input after the button is pressed"""
QMessageBox.about(self, "I want only this!!", "I want only you and not the next page!!")
class ProcessData(QWizardPage):
""" Sensor Code Entry """
def __init__(self, parent=None):
super(ProcessData, self).__init__(parent)
# num of logs combo box
self.num_logs_combo = QComboBox(self)
# ~buttons
self.btn = QPushButton('OK')
layout = QVBoxLayout()
layout.addWidget(self.num_logs_combo)
layout.addWidget(self.btn)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
wizard = Wizard()
wizard.show()
sys.exit(app.exec_())
如果运行上面的代码并单击ok
,则将保留在页面上。如果您选择了QLineEdit
框以外的任何内容,也会发生同样的事情。
如果您在QLineEdit
框中并按Enter
,则将转到下一页并显示消息框。
如何阻止Enter Key
链接到Next
按钮。
如何访问和覆盖BACK
中NEXT
,FINISH
和QWizard
按钮的属性?
答案 0 :(得分:2)
要访问按钮,必须使用button()
方法并传递QWizard::WizardButton
,在这种情况下,必须禁用QPushButton的default
。
class Wizard(QWizard):
def __init__(self, parent=None):
super(Wizard, self).__init__(parent)
self.addPage(EnterToken(self))
self.addPage(ProcessData(self))
def showEvent(self, event):
self.button(QWizard.NextButton).setDefault(False)
super(Wizard, self).showEvent(event)
更新:
class Wizard(QWizard):
def __init__(self, parent=None):
super(Wizard, self).__init__(parent)
self.addPage(EnterToken(self))
self.addPage(ProcessData(self))
self.buttons = [self.button(t) for t in (QWizard.NextButton, QWizard.FinishButton)]
for btn in self.buttons:
btn.installEventFilter(self)
def eventFilter(self, obj, event):
if obj in self.buttons and event.type() == QEvent.Show:
obj.setDefault(False)
return super(Wizard, self).eventFilter(obj, event)