tags:

views:

55

answers:

1

I've been trying to make my classes completely self contained, but I'm having some problems, which are probably coming from my missing something that everybody else knew first off...

Anyway, take this example:

class Main_Window (QtGui.QMainWindow):
    def __init__ (self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_bookingSystemMain()
        self.ui.setupUi(self)

        # Connect slots
        QtCore.QObject.connect(self.ui.submitRecord, QtCore.SIGNAL("clicked()"), self.__clickSubmitRecord)
        QtCore.QObject.connect(self.ui.btnListBookings, QtCore.SIGNAL("clicked()"), self.__show_list)

    def __clickSubmitRecord (self):
        global bookings

        name = self.ui.edtName.text()
        event = str(self.ui.comEvent.currentText())
        amount = self.ui.spinBox.value()

        if name == '':
            QtGui.QMessageBox.warning(self, "Error", "Please enter a name!")
        elif amount == 0:
            QtGui.QMessageBox.warning(self, "Error", "You can't reserve 0 tickets!")
        elif event == '':
            QtGui.QMessageBox.warning(self, "Error", "Please choose an event!")
        else:
            bookings.append(Booking(name, event, amount))
            QtGui.QMessageBox.information(self, "Booking added", "Your booking for " + str(amount) + " ticket(s) to see " + event + " in the name of " + name + " was sucessful.")
            self.__clear_widgets()

    def __clear_widgets (self):
        self.ui.edtName.clear()
        self.ui.comEvent.setCurrentIndex(-1)
        self.ui.spinBox.setValue(0)

    def __show_list (self):
        listdialog = List_Window(self)
        listdialog.show()

Which implements a UI described in another module. The clickSubmitRecord() method uses the global 'booking' list and adds to it - now surely this class shouldn't have anything to do with anything other than that UI?

How would I implement this in a good ood way? As I said I'm probably missing some kind of technique or obvious design feature...

Thanks!

+1  A: 

I don't know Python so I can't give a good example here, but what I would probably do with Qt in C++ is define a signal of "bookingAdded" to your window object, and have one of your external objects (probably whichever invokes the UI) connect a slot to this signal, and then in your clickSubmitRecord you fire this signal and the new booking data is passed with the signal to your external object.

Then your UI object doesn't need to know about anything external, and all your external objects need to know about the UI is the signal that it exposes.

This can also help with thread safety if you use a queued connection to the signal.

Gerald