2010-01-02 66 views
0

我有一个分层结构的两个组合框。第一个组合框显示客户名单的列表,即来自MySQL数据库的不同公司。每个客户在不同城市都有分支机构。Python分层结构QcomboBox:清理列表

然后,当从combo-box1选项列表中选择客户名称时,例如, {Aldi,Meyer,Carrefour,WalMart},对于该特定客户,城市/分行列表将自动显示在组合框2中。类似的东西,例如:

combo1: chosen_customer [Aldi] --> cities:{NY, Boston, Berlin, Tokyo, London} then.. 
combo2: options {NY, Boston, Berlin, Tokyo, London} 

问题是当我们再次选择了另一个客户,最终有分支机构的数量较少 - 例如

combo1: chosen_customer [Meyer] --> {LA, San Francisco}, then.. we got 
combo2: options {LA, San Francisco, Berlin, Tokyo, London} 

intead of combo2: options {LA, San Francisco} 

这里是运行combo2,这就是所谓的每一个客户名称从列表中COMBO1选择时间的函数:

def loadComboCity(self,customerName): 
    """query results cityList into self.mydb.matrix""" 
    queryName="citylist_thisCustomer" 
    self.mysqlAPI(queryName,customerName) 

    id=0 
    for row in self.mydb.matrix: 
     cityname=self.mydb.matrix[id][0] 
     self.addcomboCity(id,cityname) 
     id=id+1 
    del self.mydb.matrix[:] 

和功能,增加了属于列表城市的每一个名字对该客户:

def addcomboCity(self,id,cityname): 
    self.comboCity.addItem(QtCore.QString()) 
    self.comboCity.setItemText(id, QtGui.QApplication.translate("MainWindow", cityname, None, QtGui.QApplication.UnicodeUTF8)) 

我们试图用del来清理列表中的前一个内容,但它仍然得到相同的行为。

这是一个Qt或Python相关的问题?或者有一些我们在这里失踪?

所有的意见和建议,非常感谢。

回答

1

我想你在'for循环'之前缺少一个QComboBox.clear()调用。尝试以下代码(注意id = 0之前的新行)

def loadComboCity(self,customerName): 
    """query results cityList into self.mydb.matrix""" 
    queryName="citylist_thisCustomer" 
    self.mysqlAPI(queryName,customerName) 

    # Clear the previous items in the combobox (if any) 
    self.comboCity.clear() 

    id=0 
    for row in self.mydb.matrix: 
     cityname=self.mydb.matrix[id][0] 
     self.addcomboCity(id,cityname) 
     id=id+1 
    del self.mydb.matrix[:] 
+0

Thanks Apt! 'clear()'解决了这个问题........ |:0), – ThreaderSlash 2010-01-03 06:36:42