pyqt:how to emit a signal when NextButton of a QwizardPage was clicked

1.7k views Asked by At

I created a QWizard object which contains several pages, I tried to open a file when NextButton of the specific page was clicked. At first, I triedQWizard.NextButton.clicked.connect(), came with : "AttributeError: 'WizardButton' object has no attribute 'clicked''.

After that, I searched "WizardButton" in Qt Assistant, couldn't get any useful signals to emit, is it possible to implement that like QPushButton.clicked.connect()?

here's the specific page:

class AccountPage(QWizardPage):

    def __init__(self, parent=None):
        super(AccountPage, self).__init__(parent)

        self.setTitle("Account Information")

        NameLabel = QLabel("&Name:")
        NameLineEdit = QLineEdit()
        NameLabel.setBuddy(NameLineEdit)

        EmailLabel = QLabel("&Email Address:")
        EmailLineEdit = QLineEdit()
        EmailLabel.setBuddy(EmailLineEdit)

        PwdLabel = QLabel("&Password:")
        PwdLineEdit = QLineEdit()
        PwdLabel.setBuddy(PwdLineEdit)

        self.registerField('Name*', NameLineEdit)
        self.registerField('EmailAddress*', EmailLineEdit)
        self.registerField('Password*', PwdLineEdit)

        layout = QGridLayout()
        layout.addWidget(NameLabel, 0, 0)
        layout.addWidget(NameLineEdit, 0, 1)
        layout.addWidget(EmailLabel, 1, 0)
        layout.addWidget(EmailLineEdit, 1, 1)
        layout.addWidget(PwdLabel, 2, 0)
        layout.addWidget(PwdLineEdit, 2, 1)

        self.setLayout(layout)

        QWizard.NextButton.clicked.connect(self.setMB)

    def setMB(self):
        try:
            with open('mb.ini', 'w') as actInfo:
                actInfo.write('sj')

        except IOError as err:
            print('mb.ini error:' + str(err))
1

There are 1 answers

4
Safoyeth On BEST ANSWER
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Window(QWizard):  
    def __init__(self):
        super(Window, self).__init__()
        self.accoutPage = AccountPage()
        self.secondPage = Page2()
        self.thirdPage = Page3()
        self.addPage(self.accoutPage)
        self.addPage(self.secondPage)
        self.addPage(self.thirdPage)
        self.button(QWizard.NextButton).clicked.connect(self.accoutPage.setMB)


class AccountPage(QWizardPage):
    def __init__(self, parent=None):
        super(AccountPage, self).__init__(parent)

        self.setTitle("Account Information")

        self.NameLabel = QLabel("&Name:")
        self.NameLineEdit = QLineEdit()
        self.NameLabel.setBuddy(self.NameLineEdit)

        self.EmailLabel = QLabel("&Email Address:")
        self.EmailLineEdit = QLineEdit()
        self.EmailLabel.setBuddy(self.EmailLineEdit)

        self.PwdLabel = QLabel("&Password:")
        self.PwdLineEdit = QLineEdit()
        self.PwdLabel.setBuddy(self.PwdLineEdit)

        self.registerField('Name*', self.NameLineEdit)
        self.registerField('EmailAddress*', self.EmailLineEdit)
        self.registerField('Password*', self.PwdLineEdit)


        layout = QGridLayout()
        layout.addWidget(self.NameLabel, 0, 0)
        layout.addWidget(self.NameLineEdit, 0, 1)
        layout.addWidget(self.EmailLabel, 1, 0)
        layout.addWidget(self.EmailLineEdit, 1, 1)
        layout.addWidget(self.PwdLabel, 2, 0)
        layout.addWidget(self.PwdLineEdit, 2, 1)

        self.setLayout(layout)
        self.i = 0 #if you need to call it once

    def setMB(self):
        if self.i == 0: 
            print(self.PwdLineEdit.text(), self.i) 
            try:
                with open('mb.ini', 'w') as actInfo:
                    actInfo.write('sj')

            except IOError as err:
                print('mb.ini error:' + str(err))
            finally:
                self.i += 1


class Page2(QWizardPage):
    def __init__(self):
        super(Page2, self).__init__()

class Page3(QWizardPage):
    def __init__(self):
        super(Page3, self).__init__()

def main():
    app = QApplication(sys.argv)
    app.setStyle('plastique')

    window = Window()
    window.setWizardStyle(1)
    window.show()
    app.exec_()  

if __name__ == "__main__":
    sys.exit(main())