Issue
I am making a Wizard in QWizard
I have QLineEdit
and 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.
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.
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.
How can I stop the Enter Key
from being linked to the Next
button.
How can I access and override attributes for the BACK
, NEXT
and FINISH
buttons in QWizard
?
Solution
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.
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)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.