Qt的自包含课程

时间:2009-10-10 16:21:02

标签: python oop

我一直在试图让我的课程完全自成一体,但我遇到了一些问题,这些问题可能来自我遗漏的其他人先知道的事情......

无论如何,举个例子:

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

它实现了另一个模块中描述的UI。 clickSubmitRecord()方法使用全局“预订”列表并添加到它 - 现在肯定这个类不应该与该UI之外的任何事情有任何关系吗?

我如何以良好的方式实现这一目标?正如我所说,我可能缺少某种技术或明显的设计特征......

谢谢!

1 个答案:

答案 0 :(得分:1)

我不懂Python,所以我不能在这里给出一个很好的例子,但是我在C ++中用Qt做的事情是为你的window对象定义一个“bookingAdded”的信号,并且有一个你的外部对象(可能以调用UI为准)将插槽连接到此信号,然后在clickSubmitRecord中触发此信号,新的预订数据将与信号一起传递到外部对象。

然后你的UI对象不需要知道外部的任何东西,所有你的外部对象需要知道UI是它所暴露的信号。

如果您使用排队连接信号,这也有助于线程安全。