PyQt signals/slots dialogs question

A

AlienBaby

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.
 
P

Phil Thompson

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
 
A

AlienBaby

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?
 
A

AlienBaby

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.
 
A

AlienBaby

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.
 
A

AlienBaby

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_())
 
T

Thomas Jollans

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top