0

Im trying to build an app and to have a wizard with it. i have created the GUI with pyqt5 and used the Qwizard libary for the wizard. im trying to connect a method from a qwizard page to the Next button but cant seem to find a way.

I wish to connect the method in class page2 called Next button , i wish to trigger something when next button on this page is clicked.

My code:

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QToolBar, QAction, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel,\
    QGroupBox, QWizard, QWizardPage, QPushButton, QLineEdit, QComboBox
import PyQt5.QtGui as QtGui
from PyQt5.QtGui import QFont


class App(QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.left = 200
        self.top = 200
        self.width = 640
        self.height = 480
        self.title = "App"
        self.setWindowTitle(self.title)
        self.setWindowIcon(QtGui.QIcon("logo.ico"))
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.toolbar = QToolBar("")

        ########################## ToolbarButtons ###################################
        self.button_add = QAction("Add", self)
        self.button_add.setIcon(QtGui.QIcon("add.ico"))
        self.button_add.setStatusTip("Add stuff")
        self.toolbar.addAction(self.button_add)
        self.button_browse = QAction("Open", self)
        self.button_browse.setIcon(QtGui.QIcon("folder.ico"))
        self.button_browse.setStatusTip("Open stuff")
        self.toolbar.addAction(self.button_browse)
        self.button_save = QAction("Save", self)
        self.button_save.setIcon(QtGui.QIcon("save.ico"))
        self.button_save.setStatusTip("Save stuff")
        self.toolbar.addAction(self.button_save)
        self.button_settings = QAction("Settings", self)
        self.button_settings.setIcon(QtGui.QIcon("settings.ico"))
        self.button_settings.setStatusTip("Set stuff")
        self.toolbar.addAction(self.button_settings)


        self.window_layout = QGridLayout()
        self.setLayout(self.window_layout)
        self.wizard = WizardInit()
        print("Test")
        self.wizard.setWizardStyle(QWizard.ModernStyle)
        self.show()
        self.wizard.show()


class WizardInit(QWizard):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Wizard")
        self.resize(500, 500)
        self.addPage(Page1())
        self.addPage(Page2())
        self.addPage(Page3())


class Page1(QWizardPage):
    def __init__(self):
        super().__init__()


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

    def next_btn(self):
        print("next")


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


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Udi
  • 67
  • 8
  • mmm, since pressing the "next" button changes the page so you want to run "next_btn" before or after the page change, does "next_btn" need to belong to the second page? – eyllanesc Apr 27 '20 at 12:30
  • I want it to do a procedure that will folow at page 3. – Udi Apr 27 '20 at 14:28
  • Hmm, explain yourself better, I don't understand you. You can point to the concrete example. – eyllanesc Apr 27 '20 at 14:34
  • Ok so i want when i press Next button it will open a browser (im doing it with webdriver and with a button for it today). and continue to the next page while the browser is open. – Udi Apr 27 '20 at 14:48
  • So is it necessary for "next_btn" to be implemented in "Page2"? If you want to launch the browser then why don't you do it when the third page opens? According to what you point out, I think you have an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – eyllanesc Apr 27 '20 at 14:54
  • I want also to validate in page 2.i guessed the best option is to validate in page 2 when clicking next and the put a if statment. if valid then open the browser and go to page 3 if no valid dont open browser and popup error messege. i need to interact with the next button but i cant seem to do it. – Udi Apr 27 '20 at 15:14
  • please provide a [mre] – eyllanesc Apr 27 '20 at 15:26
  • eyllanesc thanks for the replies. as you can see in my code there is a method next_btn which prints("next"), i want this method to be called upon hitting next button. thats it :) – Udi Apr 28 '20 at 05:30

1 Answers1

0

You can connect the completeChanged() signal to your method. That should do the job. Note that completeChanged() is also emitted under other circumstances (when pressing the back button for example). So make sure you test for the right button in the code.

Try the following changes in the code for a simple version you will later need to make the next_btn more complicated to ensure the right button is clicked:

class Page2(QWizardPage):
    def __init__(self):
        super().__init__()
        self.completeChanged.connect(self.next_btn)

    def next_btn(self):
        print("next")
Miguel Garcia
  • 1,029
  • 5
  • 14