Disable enterkey for next button pyqt5 QWizard
Disable enterkey for next button pyqt5 QWizard
I am making a Wizard in QWizard
QWizard
I have QLineEdit and QPushButton
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)
I have put in this line which accepts an enter key press and runs the function the same as clicking the "OK" button.
# Enter key press connection
self.enter_token_box.returnPressed.connect(self._EnterToken)
The problem is that it will trigger BOTH the OK button AND the Next button of the wizard.
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_())
If you run the code above and click ok, you will remain on the page. Same thing happens if you have anything selected other than the QLineEdit box.
ok
QLineEdit
If you are in the QLineEdit box and you press Enter, you will be taken to the next page as well as displaying the messagebox.
QLineEdit
Enter
How can I stop the Enter Key from being linked to the Next button.
Enter Key
Next
How can I access and override attributes for the BACK, NEXT and FINISH buttons in QWizard?
BACK
NEXT
FINISH
QWizard
fair point!! I have updated my answer!
– johnashu
Jul 1 at 20:18
1 Answer
1
To access the buttons you must use the button() method and pass the QWizard::WizardButton, in your case you must disable the default of the QPushButton.
button()
QWizard::WizardButton
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)
Update:
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)
I had tried both
setAutoDefault and setDefault before but it was inside the page classes!! setDefault works perfectly in the Wizard class.!– johnashu
Jul 2 at 4:55
setAutoDefault
setDefault
setDefault
Wizard
@johnashu You are correct, try both and at the time of copying, delete the incorrect one.
– eyllanesc
Jul 2 at 4:58
it works great.. how do i override the finish button?
– johnashu
Jul 2 at 5:03
I mean I added the same function both auto and without and changed NextButton to FinishButton and it still highlights the finish button..
– johnashu
Jul 2 at 5:06
@johnashu see my update
– eyllanesc
Jul 2 at 5:12
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you could provide a Minimal, Complete, and Verifiable example, I can not reproduce your problem.
– eyllanesc
Jul 1 at 19:18