2009-10-10 57 views
0

我一直试图让我的课完全独立的,但我有一些问题,这些问题可能是由我失去了一些东西,每个人都知道其他第一关来了...自包含的类使用Qt

无论如何,拿这个例子:

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

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

我该如何以良好的使用方式来实现?正如我所说我可能缺少某种技术或明显的设计功能...

谢谢!

回答

1

我不知道Python,所以我不能在这里给出一个好例子,但是我可能用Qt在C++中做的是定义一个“预订添加到”窗口对象的信号,并且有一个外部对象(可能是调用UI)将一个插槽连接到此信号,然后在您的clickSubmitRecord中触发该信号,并将新的预订数据与信号一起传递给您的外部对象。

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

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