How do I show a Test_Question (String) per page?? (PyQt/Python)
Question: I am creating a PyQt application which creates and sets tests for the user.
However, when setting the test questions i wanted 1 question to be viewed per
page. For example, if the user was to start a test, then a question would
appear. Once they have answered that question, then they click a button
`'Proceed'` which presents the next page showing only the 2nd question and so
on... However, in my case, i am unaware as to how many questions there are in
each test, so the number of pages will vary. I am using switching layouts as i
would rather switch layouts as opposed to open new windows but that means that
i have to create a class per question and because questions per test vary, i
am unsure as to how i will present the questions????

I am not going to copy out my entire application code but will provide a basic
example below:

    
    
    import sys
    from PyQt4 import QtCore, QtGui
    
    class StartTest(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(StartTest, self).__init__(parent)
            self.central_widget = QtGui.QStackedWidget()
            self.setCentralWidget(self.central_widget)
            question1 = Question1(self)
            self.central_widget.addWidget(question1)
            self.central_widget.setCurrentWidget(question1)
            question1.proceed.clicked.connect(self.question2)
        def question2(self):
            question2 = Question2(self)
            self.central_widget.addWidget(question2)
            self.central_widget.setCurrentWidget(question2)
    
    class Question1(QtGui.QWidget):
        def __init__(self, parent=None):
            super(Question1, self).__init__(parent)
            question = QtGui.QLabel('What is 5+5?')
            self.proceed = QtGui.QPushButton("Proceed")
            self.Answer = QtGui.QLineEdit(self)
            layout = QtGui.QFormLayout()
            layout.addRow(question, self.Answer)
            layout2 = QtGui.QVBoxLayout()
            layout2.addLayout(layout)
            layout2.addWidget(self.proceed)
            self.setLayout(layout2)
    
    class Question2(QtGui.QWidget):
        def __init__(self, parent=None):
            super(Question2, self).__init__(parent)
            question = QtGui.QLabel('What is 45+10?')
            self.proceed = QtGui.QPushButton("Proceed")
            self.Answer = QtGui.QLineEdit(self)
            layout = QtGui.QFormLayout()
            layout.addRow(question, self.Answer)
            layout2 = QtGui.QVBoxLayout()
            layout2.addLayout(layout)
            layout2.addWidget(self.proceed)
            self.setLayout(layout2)
    #....
    if __name__ == '__main__':
        User = ''
        app = QtGui.QApplication([])
        window = StartTest()
        window.showFullScreen()
        app.exec_()
    


Answer: Just have a single `Question` class that can be parameterized. For the example
code in the question, this would be as simple as passing the label text as an
argument, since this is the only thing that is different:

    
    
    class Question(QtGui.QWidget):
        def __init__(self, label, parent=None):
            super(Question, self).__init__(parent)
            question = QtGui.QLabel(label)
            ...
    
    question1 = Question('What is 5+5?', self)
    question2 = Question('What is 45+10?', self)
    

If some question types have a different structure, you could create subclasses
for each type:

    
    
    class ComplexQuestion(Question):
        def __init__(self, label, parent=None):
            super(ComplexQuestion, self).__init__(parent)
            # do additional initialization...
    

Or if you wanted to avoid subclassing, you could simply add methods to the
`Question` class that enabled/disabled certain features.

