PyQt signals/slots dialogs question

Discussion in 'Python' started by AlienBaby, Jun 7, 2010.

  1. AlienBaby

    AlienBaby Guest

    Hi,

    I'm just starting to get to grips with PyQt, and I'm having a bit of
    trouble connecting slots / signals, or understanding how I should do
    so to achieve what I am after.

    I am trying to write an application that will display a sequence of
    dialogs, with back / next / cancel buttons to step through the
    dialogs. Each dialog will capture user input, ask the user to browse
    for a file, present some radio-buttons for a selection etc..

    I've put the dialogs together in Designer, and I come to startup the
    app with a small piece of python code, create an instance of each
    dialog, and then connect up the buttons to any methods needed to
    control the movement through the dialogs etc.

    I'm having a brain-freeze though, and just can't figure out what I
    should be doing next, or the best approach to take.

    for example, Currently I am thinking I would need to connect the
    clicked() signal of the Next button on dialog 1 to a method say,
    'dialog1Next' that would then hide dialog1 and show dialog2. I would
    then need to connect Dialog 2's clicked() signal of its next button to
    a method called 'dialog2Next' that hides dialog2 and shows dialog3
    etc...

    I would also, for example, need methods for dialog2Back,
    dialog2cancel, dialog1cancel etc..

    I'm having trouble figuring out how to setup the connections, and
    where the slot-methods should live..

    I have this so far;

    from PyQt4 import QtCore, QtGui
    from WELCOME import Ui_Dialog as welcomeDialog
    from SOURCE_TYPE_SELECT import Ui_Dialog as sourceTypeSelectDialog
    from SOURCE_TYPE_CONFIRMATION import Ui_Dialog as
    sourceTypeConfirmationDialog
    from ACCEPT_EULA import Ui_Dialog as acceptEulaDialog
    from CONFIRM_REQUIREMENTS import Ui_Dialog as
    confirmRequirementsDialog
    from SOURCE_BINARY_LOCATE import Ui_Dialog as sourceBinaryLocateDialog
    from SOURCE_BINARY_PROBLEMS import Ui_Dialog as
    sourceBinaryProblemsDialog
    from OUTFILE_LOCATE import Ui_Dialog as outfileLocateDialog
    from OUTFILE_PROBLEMS import Ui_Dialog as outfileProblemsDialog
    from COLLECTION_PROGRESS import Ui_Dialog as collectionProgressDialog
    from OUTFILE_LOCATION import Ui_Dialog as outfileLocationDialog



    class StartQT4(QtGui.QDialog):
    def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)
    self.Welcome = welcomeDialog()
    self.Welcome.setupUi(self)
    self.SourceTypeSelect = sourceTypeSelectDialog()
    self.SourceTypeConfirmation = sourceTypeConfirmationDialog()
    self.AcceptEula = acceptEulaDialog()
    self.ConfirmRequirements=confirmRequirementsDialog()
    self.SourceBinaryLocate=sourceBinaryLocateDialog()
    self.SourceBinaryProblems=sourceBinaryProblemsDialog()
    self.OutfileLocate=outfileLocateDialog()
    self.OutfileProbelms=outfileProblemsDialog()
    self.CollectionProgress=collectionProgressDialog()
    self.OutfileLocation=outfileLocationDialog

    #Connect up next/back/cancel etc.. buttons

    ??? self.Welcome.connect(self.Welcome.welcomeNext,
    QtCore.SIGNAL("clicked()"),WelcomeNext)



    def WelcomeNext():
    #Code here to hide Welcome dialog and show SourceTypeSelect
    dialog



    if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())





    My questions are, am I approaching this the right way, with a method
    per action per dialog to move through the dialogs, and just how do I
    setup the connections I need. I'm getting confused with scope etc. IE:
    if the connect is right, and WelcomeNext fires, how can it refer to
    myapp.SourceTypeSelect.show() or myapp.Welcome.hide()


    Any help much appreciated.
    AlienBaby, Jun 7, 2010
    #1
    1. Advertising

  2. On Mon, 7 Jun 2010 08:22:07 -0700 (PDT), AlienBaby
    <>
    wrote:
    > Hi,
    >
    > I'm just starting to get to grips with PyQt, and I'm having a bit of
    > trouble connecting slots / signals, or understanding how I should do
    > so to achieve what I am after.
    >
    > I am trying to write an application that will display a sequence of
    > dialogs, with back / next / cancel buttons to step through the
    > dialogs. Each dialog will capture user input, ask the user to browse
    > for a file, present some radio-buttons for a selection etc..
    >
    > I've put the dialogs together in Designer, and I come to startup the
    > app with a small piece of python code, create an instance of each
    > dialog, and then connect up the buttons to any methods needed to
    > control the movement through the dialogs etc.
    >
    > I'm having a brain-freeze though, and just can't figure out what I
    > should be doing next, or the best approach to take.
    >
    > for example, Currently I am thinking I would need to connect the
    > clicked() signal of the Next button on dialog 1 to a method say,
    > 'dialog1Next' that would then hide dialog1 and show dialog2. I would
    > then need to connect Dialog 2's clicked() signal of its next button to
    > a method called 'dialog2Next' that hides dialog2 and shows dialog3
    > etc...
    >
    > I would also, for example, need methods for dialog2Back,
    > dialog2cancel, dialog1cancel etc..
    >
    > I'm having trouble figuring out how to setup the connections, and
    > where the slot-methods should live..
    >
    > I have this so far;
    >
    > from PyQt4 import QtCore, QtGui
    > from WELCOME import Ui_Dialog as welcomeDialog
    > from SOURCE_TYPE_SELECT import Ui_Dialog as sourceTypeSelectDialog
    > from SOURCE_TYPE_CONFIRMATION import Ui_Dialog as
    > sourceTypeConfirmationDialog
    > from ACCEPT_EULA import Ui_Dialog as acceptEulaDialog
    > from CONFIRM_REQUIREMENTS import Ui_Dialog as
    > confirmRequirementsDialog
    > from SOURCE_BINARY_LOCATE import Ui_Dialog as sourceBinaryLocateDialog
    > from SOURCE_BINARY_PROBLEMS import Ui_Dialog as
    > sourceBinaryProblemsDialog
    > from OUTFILE_LOCATE import Ui_Dialog as outfileLocateDialog
    > from OUTFILE_PROBLEMS import Ui_Dialog as outfileProblemsDialog
    > from COLLECTION_PROGRESS import Ui_Dialog as collectionProgressDialog
    > from OUTFILE_LOCATION import Ui_Dialog as outfileLocationDialog
    >
    >
    >
    > class StartQT4(QtGui.QDialog):
    > def __init__(self, parent=None):
    > QtGui.QWidget.__init__(self, parent)
    > self.Welcome = welcomeDialog()
    > self.Welcome.setupUi(self)
    > self.SourceTypeSelect = sourceTypeSelectDialog()
    > self.SourceTypeConfirmation = sourceTypeConfirmationDialog()
    > self.AcceptEula = acceptEulaDialog()
    > self.ConfirmRequirements=confirmRequirementsDialog()
    > self.SourceBinaryLocate=sourceBinaryLocateDialog()
    > self.SourceBinaryProblems=sourceBinaryProblemsDialog()
    > self.OutfileLocate=outfileLocateDialog()
    > self.OutfileProbelms=outfileProblemsDialog()
    > self.CollectionProgress=collectionProgressDialog()
    > self.OutfileLocation=outfileLocationDialog
    >
    > #Connect up next/back/cancel etc.. buttons
    >
    > ??? self.Welcome.connect(self.Welcome.welcomeNext,
    > QtCore.SIGNAL("clicked()"),WelcomeNext)
    >
    >
    >
    > def WelcomeNext():
    > #Code here to hide Welcome dialog and show SourceTypeSelect
    > dialog
    >
    >
    >
    > if __name__ == "__main__":
    > app = QtGui.QApplication(sys.argv)
    > myapp = StartQT4()
    > myapp.show()
    > sys.exit(app.exec_())
    >
    >
    >
    >
    >
    > My questions are, am I approaching this the right way, with a method
    > per action per dialog to move through the dialogs, and just how do I
    > setup the connections I need. I'm getting confused with scope etc. IE:
    > if the connect is right, and WelcomeNext fires, how can it refer to
    > myapp.SourceTypeSelect.show() or myapp.Welcome.hide()
    >
    >
    > Any help much appreciated.


    Sounds like you are trying to build a wizard the hard way.

    Use Designer to create a QWizard with the contents of each of your dialogs
    as a QWizardPage.

    Phil
    Phil Thompson, Jun 7, 2010
    #2
    1. Advertising

  3. AlienBaby

    AlienBaby Guest

    I've made a little progress;

    This appears to setup a connection that fires when the welcomeNext
    button in the Welcome dialog is clicked;

    self.Welcome.welcomeNext.connect(self.Welcome.welcomeNext,QtCore.SIGNAL("clicked()"),self.WelcomeNext)


    In the StartQT4 class I now have the WelcomeNext method;

    def WelcomeNext(self):
    self.Welcome.setVisible(False)
    self.SourceTypeSelect.setVisible(True)

    Though I am now getting

    AttributeError: 'Ui_Dialog' object has no attribute 'setVisible'

    Which, is right. Because it's not really a QDialog class.

    Instead of using a 'WelcomeNext' method as the slot, should I just
    connect the clicked() signal to both the hide slot of the welcome
    dialog and the show slot of the SourceTypeSelect dialog? .. though
    then where would any code I wanted to run say, (for the sake of
    argument) between the hiding of one dialog and the showing of the
    next?
    AlienBaby, Jun 7, 2010
    #3
  4. AlienBaby

    AlienBaby Guest

    On Jun 7, 4:37 pm, Phil Thompson <> wrote:
    > On Mon, 7 Jun 2010 08:22:07 -0700 (PDT), AlienBaby
    > <>
    > wrote:
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > I'm just starting to get to grips with PyQt, and I'm having a bit of
    > > trouble connecting slots / signals, or understanding how I should do
    > > so to achieve what I am after.

    >
    > > I am trying to write an application that will display a sequence of
    > > dialogs, with back / next / cancel buttons to step through the
    > > dialogs. Each dialog will capture user input, ask the user to browse
    > > for a file, present some radio-buttons for a selection etc..

    >
    > > I've put the dialogs together in Designer, and I come to startup the
    > > app with a small piece of python code, create an instance of each
    > > dialog, and then connect up the buttons to any methods needed to
    > > control the movement through the dialogs etc.

    >
    > > I'm having a brain-freeze though, and just can't figure out what I
    > > should be doing next, or the best approach to take.

    >
    > > for example, Currently I am thinking I would need to connect the
    > > clicked() signal of the Next button on dialog 1 to a method say,
    > > 'dialog1Next' that would then hide dialog1 and show dialog2.  I would
    > > then need to connect Dialog 2's clicked() signal of its next button to
    > > a method called 'dialog2Next' that hides dialog2 and shows dialog3
    > > etc...

    >
    > > I would also, for example,  need methods for dialog2Back,
    > > dialog2cancel, dialog1cancel etc..

    >
    > > I'm having trouble figuring out how to setup the connections, and
    > > where the slot-methods should live..

    >
    > > I have this so far;

    >
    > > from PyQt4 import QtCore, QtGui
    > > from WELCOME import Ui_Dialog as welcomeDialog
    > > from SOURCE_TYPE_SELECT import Ui_Dialog as sourceTypeSelectDialog
    > > from SOURCE_TYPE_CONFIRMATION import Ui_Dialog as
    > > sourceTypeConfirmationDialog
    > > from ACCEPT_EULA import Ui_Dialog as acceptEulaDialog
    > > from CONFIRM_REQUIREMENTS import Ui_Dialog as
    > > confirmRequirementsDialog
    > > from SOURCE_BINARY_LOCATE import Ui_Dialog as sourceBinaryLocateDialog
    > > from SOURCE_BINARY_PROBLEMS import Ui_Dialog as
    > > sourceBinaryProblemsDialog
    > > from OUTFILE_LOCATE import Ui_Dialog as outfileLocateDialog
    > > from OUTFILE_PROBLEMS import Ui_Dialog as outfileProblemsDialog
    > > from COLLECTION_PROGRESS import Ui_Dialog as collectionProgressDialog
    > > from OUTFILE_LOCATION import Ui_Dialog as outfileLocationDialog

    >
    > > class StartQT4(QtGui.QDialog):
    > >     def __init__(self, parent=None):
    > >         QtGui.QWidget.__init__(self, parent)
    > >         self.Welcome = welcomeDialog()
    > >         self.Welcome.setupUi(self)
    > >         self.SourceTypeSelect = sourceTypeSelectDialog()
    > >         self.SourceTypeConfirmation = sourceTypeConfirmationDialog()
    > >         self.AcceptEula = acceptEulaDialog()
    > >         self.ConfirmRequirements=confirmRequirementsDialog()
    > >         self.SourceBinaryLocate=sourceBinaryLocateDialog()
    > >         self.SourceBinaryProblems=sourceBinaryProblemsDialog()
    > >         self.OutfileLocate=outfileLocateDialog()
    > >         self.OutfileProbelms=outfileProblemsDialog()
    > >         self.CollectionProgress=collectionProgressDialog()
    > >         self.OutfileLocation=outfileLocationDialog

    >
    > >         #Connect up next/back/cancel etc.. buttons

    >
    > > ???     self.Welcome.connect(self.Welcome.welcomeNext,
    > > QtCore.SIGNAL("clicked()"),WelcomeNext)

    >
    > > def WelcomeNext():
    > >         #Code here to hide Welcome dialog and show SourceTypeSelect
    > > dialog

    >
    > > if __name__ == "__main__":
    > >    app = QtGui.QApplication(sys.argv)
    > >    myapp = StartQT4()
    > >    myapp.show()
    > >    sys.exit(app.exec_())

    >
    > > My questions are, am I approaching this the right way, with a method
    > > per action per dialog to move through the dialogs, and just how do I
    > > setup the connections I need. I'm getting confused with scope etc. IE:
    > > if the connect is right, and WelcomeNext fires, how can it refer to
    > > myapp.SourceTypeSelect.show() or myapp.Welcome.hide()

    >
    > > Any help much appreciated.

    >
    > Sounds like you are trying to build a wizard the hard way.
    >
    > Use Designer to create a QWizard with the contents of each of your dialogs
    > as a QWizardPage.
    >
    > Phil- Hide quoted text -
    >
    > - Show quoted text -


    Interesting idea. I shall take a look, but there is q fair amount of
    work that needs to be done between dialogs, validation and other
    thoings etc.. Would a QWizard help?


    As well, I would like to understand the process of coding my own so I
    can build other UI's also.
    AlienBaby, Jun 7, 2010
    #4
  5. AlienBaby

    AlienBaby Guest

    My real aim here is to learn pyqt, so I would rather not the the
    QWizard process until I understand myself whats going on behind the
    scenes.
    AlienBaby, Jun 7, 2010
    #5
  6. AlienBaby

    AlienBaby Guest

    On Jun 7, 5:21 pm, AlienBaby <> wrote:
    > My real aim here is to learn pyqt, so I would rather not the the
    > QWizard process until I understand myself whats going on behind the
    > scenes.


    Perhaps I posted to early, but a little more perserverance and I have
    managed to unthaw the brain and move forward. I've ended up with the
    following. It does what I need, and now it's working I can think
    around how to implement with a better approach;


    from PyQt4 import QtCore, QtGui
    from WELCOME import Ui_Dialog as welcomeDialog
    from SOURCE_TYPE_SELECT import Ui_Dialog as sourceTypeSelectDialog
    from SOURCE_TYPE_CONFIRMATION import Ui_Dialog as
    sourceTypeConfirmationDialog

    ...>snip<

    class WelcomeDialog(QtGui.QDialog):
    def __init__(self,parent=None):
    QtGui.QWidget.__init__(self,parent)
    self.ui=welcomeDialog()
    self.ui.setupUi(self)

    class SourceTypeSelect(QtGui.QDialog):
    def __init__(self,parent=None):
    QtGui.QWidget.__init__(self,parent)
    self.ui=sourceTypeSelectDialog()
    self.ui.setupUi(self)

    class SourceTypeConfirmation(QtGui.QDialog):
    def __init__(self,parent=None):
    QtGui.QWidget.__init__(self,parent)
    self.ui=sourceTypeConfirmationDialog()
    self.ui.setupUi(self)


    def WelcomeNext():
    welcome.hide()
    sourceTypeSelect.show()
    def WelcomeCancel():
    sys.exit()


    def SourceTypeSelectNext():
    sourceTypeSelect.hide()
    for widget in
    sourceTypeSelect.ui.availableChoices.findChildren(QtGui.QWidget):
    if widget.isChecked():
    print widget.text()

    sourceTypeConfirmation.ui.selectedSourceType.setText(widget.text())
    sourceTypeConfirmation.show()
    def SourceTypeSelectCancel():
    sys.exit()


    def SourceTypeConfirmationBack():
    sourceTypeConfirmation.hide()
    sourceTypeSelect.show()


    if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    #Instance dialogs and connect buttons
    welcome=WelcomeDialog()

    welcome.ui.welcomeNext.connect(welcome.ui.welcomeNext,QtCore.SIGNAL("clicked()"),WelcomeNext)

    welcome.ui.welcomeCancel.connect(welcome.ui.welcomeCancel,QtCore.SIGNAL("clicked()"),WelcomeCancel)


    sourceTypeSelect=SourceTypeSelect()

    sourceTypeSelect.ui.sourceTypeSelectNext.connect(sourceTypeSelect.ui.sourceTypeSelectNext,QtCore.SIGNAL("clicked()"),SourceTypeSelectNext)

    sourceTypeSelect.ui.sourceTypeSelectCancel.connect(sourceTypeSelect.ui.sourceTypeSelectCancel,QtCore.SIGNAL("clicked()"),SourceTypeSelectCancel)


    sourceTypeConfirmation=SourceTypeConfirmation()

    sourceTypeConfirmation.ui.sourceTypeConfirmationBack.connect(sourceTypeConfirmation.ui.sourceTypeConfirmationBack,QtCore.SIGNAL("clicked()"),SourceTypeConfirmationBack)


    welcome.show()
    sys.exit(app.exec_())
    AlienBaby, Jun 7, 2010
    #6
  7. On 06/07/2010 08:21 PM, AlienBaby wrote:
    > On Jun 7, 5:21 pm, AlienBaby <> wrote:
    >
    >> My real aim here is to learn pyqt, so I would rather not the the
    >> QWizard process until I understand myself whats going on behind the
    >> scenes.
    >>

    > Perhaps I posted to early, but a little more perserverance and I have
    > managed to unthaw the brain and move forward. I've ended up with the
    > following. It does what I need, and now it's working I can think
    > around how to implement with a better approach;
    >

    I haven't done much Qt programming, and all of it in C++, but anyway:

    my approach is to subclass QFrame (or, here, QDialog makes sense), and,
    in the constructor, set up the UI and call connect(...). In C++, there's
    a connect macro -- is there a connect function in QtCore, maybe?
    "welcome.ui.welcomeNext.connect(welcome.ui.welcomeNext, ..." doesn't
    look right.
    >
    > from PyQt4 import QtCore, QtGui
    > from WELCOME import Ui_Dialog as welcomeDialog
    > from SOURCE_TYPE_SELECT import Ui_Dialog as sourceTypeSelectDialog
    > from SOURCE_TYPE_CONFIRMATION import Ui_Dialog as
    > sourceTypeConfirmationDialog
    >
    > ..>snip<
    >
    > class WelcomeDialog(QtGui.QDialog):
    > def __init__(self,parent=None):
    > QtGui.QWidget.__init__(self,parent)
    > self.ui=welcomeDialog()
    > self.ui.setupUi(self)
    >
    > class SourceTypeSelect(QtGui.QDialog):
    > def __init__(self,parent=None):
    > QtGui.QWidget.__init__(self,parent)
    > self.ui=sourceTypeSelectDialog()
    > self.ui.setupUi(self)
    >
    > class SourceTypeConfirmation(QtGui.QDialog):
    > def __init__(self,parent=None):
    > QtGui.QWidget.__init__(self,parent)
    > self.ui=sourceTypeConfirmationDialog()
    > self.ui.setupUi(self)
    >
    >
    > def WelcomeNext():
    > welcome.hide()
    > sourceTypeSelect.show()
    > def WelcomeCancel():
    > sys.exit()
    >
    >
    > def SourceTypeSelectNext():
    > sourceTypeSelect.hide()
    > for widget in
    > sourceTypeSelect.ui.availableChoices.findChildren(QtGui.QWidget):
    > if widget.isChecked():
    > print widget.text()
    >
    > sourceTypeConfirmation.ui.selectedSourceType.setText(widget.text())
    > sourceTypeConfirmation.show()
    > def SourceTypeSelectCancel():
    > sys.exit()
    >
    >
    > def SourceTypeConfirmationBack():
    > sourceTypeConfirmation.hide()
    > sourceTypeSelect.show()
    >
    >
    > if __name__ == "__main__":
    > app = QtGui.QApplication(sys.argv)
    >
    > #Instance dialogs and connect buttons
    > welcome=WelcomeDialog()
    >
    > welcome.ui.welcomeNext.connect(welcome.ui.welcomeNext,QtCore.SIGNAL("clicked()"),WelcomeNext)
    >
    > welcome.ui.welcomeCancel.connect(welcome.ui.welcomeCancel,QtCore.SIGNAL("clicked()"),WelcomeCancel)
    >
    >
    > sourceTypeSelect=SourceTypeSelect()
    >
    > sourceTypeSelect.ui.sourceTypeSelectNext.connect(sourceTypeSelect.ui.sourceTypeSelectNext,QtCore.SIGNAL("clicked()"),SourceTypeSelectNext)
    >
    > sourceTypeSelect.ui.sourceTypeSelectCancel.connect(sourceTypeSelect.ui.sourceTypeSelectCancel,QtCore.SIGNAL("clicked()"),SourceTypeSelectCancel)
    >
    >
    > sourceTypeConfirmation=SourceTypeConfirmation()
    >
    > sourceTypeConfirmation.ui.sourceTypeConfirmationBack.connect(sourceTypeConfirmation.ui.sourceTypeConfirmationBack,QtCore.SIGNAL("clicked()"),SourceTypeConfirmationBack)
    >
    >
    > welcome.show()
    > sys.exit(app.exec_())
    >
    >
    >
    >
    >
    Thomas Jollans, Jun 7, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Bossy
    Replies:
    1
    Views:
    459
    Victor Bazarov
    Jul 9, 2003
  2. tin gherdanarra

    slots? SLOTS?

    tin gherdanarra, Oct 12, 2005, in forum: Python
    Replies:
    2
    Views:
    2,299
    Peter Hansen
    Oct 13, 2005
  3. Christian Bruckhoff

    Problems with Signals & Slots of QT

    Christian Bruckhoff, Sep 24, 2006, in forum: C++
    Replies:
    5
    Views:
    405
    loufoque
    Sep 24, 2006
  4. Brian Vanderburg II

    Signals/Slots support in Python

    Brian Vanderburg II, May 1, 2008, in forum: Python
    Replies:
    0
    Views:
    258
    Brian Vanderburg II
    May 1, 2008
  5. Alex M.
    Replies:
    4
    Views:
    462
    Rolf Magnus
    Aug 2, 2010
Loading...

Share This Page