1

我正在制作一个向导QWizard

我有 QLineEditQPushButton

# 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按钮。

如何访问和覆盖 中的BACKNEXT按钮FINISH的属性QWizard

4

1 回答 1

2

要访问按钮,您必须使用该button()方法并传递QWizard::WizardButton,在您的情况下,您必须禁用defaultQPushButton 的。

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)
于 2018-07-01T20:48:42.183 回答