【问题标题】:Python Hierarchical QcomboBox: clean-up listPython Hierarchical QcomboBox:清理列表
【发布时间】:2010-12-31 17:43:16
【问题描述】:

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

然后,当从组合框 1 选项列表中选择客户名称时,例如{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 中选择 customerName 时都会调用该函数:

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 相关的问题?还是我们在这里遗漏了一些东西?

非常感谢所有 cmets 和建议。

【问题讨论】:

    标签: python qt combobox hierarchical-data


    【解决方案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[:]
    

    【讨论】:

      猜你喜欢
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多