Qt Python 组合框“currentIndexChanged”发射两次

发布于 2024-08-16 16:30:57 字数 515 浏览 3 评论 0原文

我有一个组合,表现出一些尴尬的行为。给定组合框中的选项列表,用户应该用鼠标单击选择城市的名称。代码如下:

QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)                 

def checkChosenCity(self):
    self.cityName=self.comboCity.currentText()
    print "the city chosen is:"
    print "%s" % self.cityName

问题是,每次选择一个城市时,connect 都会调用函数 checkChosenCity 两次。

该组合是分层组合,即在第一个组合中选择客户之后,第二个组合框中出现该客户的城市列表。

我希望这里有人能指出或猜测为什么会发生这种情况。

I have a combo, which is showing some awkward behavior. Given a list of options from the combo-box, the user should pick the name of a city clicking with the mouse. Here is the code:

QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)                 

def checkChosenCity(self):
    self.cityName=self.comboCity.currentText()
    print "the city chosen is:"
    print "%s" % self.cityName

The problem is, each time a city is chosen, connect calls the function checkChosenCity twice.

This combo is a hierarchical combo, i.e. after in the first combo a customer is chosen, then in the second combo-box comes the list of cities for that customer.

I hope someone here can point out or guess why this is happening.

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

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

发布评论

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

评论(2

一身仙ぐ女味 2024-08-23 16:30:57

我有完全相同的问题。经过一些调试后发现,使用

currentIndexChanged(int)

而不是

currentIndexChanged(QString)

为我解决了这个问题。

它仍然不明白为什么前者会发射两次。

I had exactly the same problem. After some debugging it turned out that using

currentIndexChanged(int)

instead of

currentIndexChanged(QString)

fixed it for me.

It still don't understand why the former fires twice.

彡翼 2024-08-23 16:30:57

谢谢 Eli..

这就是我所得到的:

combo1 : [customernames] - pick a customer.
combo2 : [cityList] - pick a city for the chosen customer.
combo3 : [emploeeList] - load employees for that city, given the chosen customer.

我发现,即使没有选择城市,城市的组合框也会被激活。是的,我已经检查过函数“checkChosenCity”是否未在程序内的其他任何地方调用。

作为一个快速修复,而不是理想的解决方案,我在函数“checkChosenCity”中添加了一个避免问题的条件。因此,现在当此功能被“连接”错误激活时,它会检查是否确实选择了城市名称,如果没有,则指向的进程不会运行,以避免系统崩溃。

这是将城市列表加载到组合框的函数:

def loadcomboCity(self,customerName):
    if customerName == " ": 
        """no customer was chosen yet - list of city empty"""
     id=0
        CityName=" "
        self.addcomboCity(id,CityName)
    else:
        """for this customerName - load his city list"""
        self.loadCityList_mysql(customerName)

     lin=0
     """ the data is imported from mysql class into self.db.matrix"""
        for row in self.db.matrix:
            id=lin
            cityname=self.db.matrix[lin][0]
            print "city name = %s" % cityname
            self.addcomboCity(id,cityname) 
            lin=lin+1

这是将客户名称列表加载到组合框的函数:

def loadComboCustomer(self):
    """queries customerList into self.connexDB.matrix"""
    self.loadCustomerList_mysql()

    lin=0
    """ the data is imported from mysql class into self.db.matrix"""
    for row in self.connexDB.matrix:
        id=lin
        customername=self.connexDB.matrix[lin][0]
        self.addcomboCustomer(id,customername)  
        lin=lin+1

这是检查是否选择了客户名称的函数:

def checkChosenCustomer(self):
    self.customerName=self.comboCustomer.currentText()
    print "the customer chosen is:"
    print "%s" % self.customerName

    self.loadcomboCity(self.customerName)

这是检查的函数如果从列表中选择某个城市到组合框:

def checkChosenCity(self):
    self.CityName=self.comboCity.currentText()
    print "the City chosen is:"
    print "value of City = %s" % self.CityName

    if self.CityName == '':
        print "empty"
    else:
        """for this city - load the respective customer employee list"""
        self.buildListOfEmployees_mysql(self.CityName)

     """ the data is imported from mysql class into self.db.matrix"""
        for row in self.db.matrix:
            id=lin+1
            personname=self.db.matrix[lin][0]
            print "person name = %s" % personname
            self.addcomboPerson(id,personname) 
            lin=lin+1

这是连接组合框事件的主要功能:

def options(self):
    self.comboCustomer = QtGui.QComboBox(self.boxBooking)
    self.comboCustomer.setGeometry(QtCore.QRect(60, 60, 521, 22))

    self.loadComboCustomer()
    QtCore.QObject.connect(self.comboCustomer, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCustomer)

    self.comboCity = QtGui.QComboBox(self.boxBooking)
    self.comboCity.setGeometry(QtCore.QRect(60, 120, 521, 22))

    self.loadcomboCity(self.customerName)
    QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)

确实不是理想的解决方案。但是,非常有趣的是,必须花费几个小时才能发现如此奇怪的连接事件被错误地自我激活。

如果您发现任何其他解释,请告诉我们。

Thanks Eli..

Here is what I have:

combo1 : [customernames] - pick a customer.
combo2 : [cityList] - pick a city for the chosen customer.
combo3 : [emploeeList] - load employees for that city, given the chosen customer.

What I find out is that, even when no city is chosen, the combox-box for city is activated. And yes, I have checked if the function 'checkChosenCity' is not called anywhere else inside the program.

As a quick fix, not the ideal solution, I put a condition to avoid the problem into the function 'checkChosenCity'. So, now when this function is wrongly activated by 'connect', it checks if a city name was really selected, if none, then the pointed process doesn't run avoiding the system to crash.

Here is the function that loads the city list into combo-box:

def loadcomboCity(self,customerName):
    if customerName == " ": 
        """no customer was chosen yet - list of city empty"""
     id=0
        CityName=" "
        self.addcomboCity(id,CityName)
    else:
        """for this customerName - load his city list"""
        self.loadCityList_mysql(customerName)

     lin=0
     """ the data is imported from mysql class into self.db.matrix"""
        for row in self.db.matrix:
            id=lin
            cityname=self.db.matrix[lin][0]
            print "city name = %s" % cityname
            self.addcomboCity(id,cityname) 
            lin=lin+1

Here is the function that loads the customer-names list into combo-box:

def loadComboCustomer(self):
    """queries customerList into self.connexDB.matrix"""
    self.loadCustomerList_mysql()

    lin=0
    """ the data is imported from mysql class into self.db.matrix"""
    for row in self.connexDB.matrix:
        id=lin
        customername=self.connexDB.matrix[lin][0]
        self.addcomboCustomer(id,customername)  
        lin=lin+1

Here is the function that checkes if the a customer name was chosen:

def checkChosenCustomer(self):
    self.customerName=self.comboCustomer.currentText()
    print "the customer chosen is:"
    print "%s" % self.customerName

    self.loadcomboCity(self.customerName)

Here is the function that checks if some city chosen from list into combo-box:

def checkChosenCity(self):
    self.CityName=self.comboCity.currentText()
    print "the City chosen is:"
    print "value of City = %s" % self.CityName

    if self.CityName == '':
        print "empty"
    else:
        """for this city - load the respective customer employee list"""
        self.buildListOfEmployees_mysql(self.CityName)

     """ the data is imported from mysql class into self.db.matrix"""
        for row in self.db.matrix:
            id=lin+1
            personname=self.db.matrix[lin][0]
            print "person name = %s" % personname
            self.addcomboPerson(id,personname) 
            lin=lin+1

Here is the main function that connect combo-box events:

def options(self):
    self.comboCustomer = QtGui.QComboBox(self.boxBooking)
    self.comboCustomer.setGeometry(QtCore.QRect(60, 60, 521, 22))

    self.loadComboCustomer()
    QtCore.QObject.connect(self.comboCustomer, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCustomer)

    self.comboCity = QtGui.QComboBox(self.boxBooking)
    self.comboCity.setGeometry(QtCore.QRect(60, 120, 521, 22))

    self.loadcomboCity(self.customerName)
    QtCore.QObject.connect(self.comboCity, QtCore.SIGNAL("currentIndexChanged(QString)"), self.checkChosenCity)

Not the ideal solution really. But, quite funny to have to spend hours to find out that such a strange connect event is being wrongly self-activated.

If you discover any other explanation, just let us know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文