Python Hierarchical QcomboBox:清理列表

发布于 2024-08-16 05:21:30 字数 1396 浏览 5 评论 0原文

我有一个分层的两个组合框。第一个组合框显示 customerNames 列表,即来自 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}

这是运行组合2的函数,每次从列表组合1中选择客户名称时都会调用该函数:

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

高度赞赏所有意见和建议。

I have a hierarchical two combo-box. The first combo-box displays a list of customerNames, i.e. different companies from a MySQL db. Each customer has branches in different cities.

Then, when a customer name is chosen from combo-box1 option list, e.g. {Aldi, Meyer, Carrefour, WalMart}, for that particular customer, a list of cities/branches is automatically displayed in the combo-box2. Something like that, e.g.:

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

The problem comes when we chose again another customer, that eventually has a smaller number of branches - e.g.

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}

Here is the function that runs the combo2, which is called every time a customerName is chosen from the list 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[:]

and the function that adds each name of the list city that belongs to that customer:

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

We tried to use del to clean the previous content of the list, but it still gets the same behavior.

This is a Qt or a Python related problem? Or there is some bit that we are missing here?

All comments and suggestions are highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-08-23 05:21:30

我认为您在“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[:]

I think you are missing a QComboBox.clear() call , before the 'for loop'. Try the following code (Notice the new line just before 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[:]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文