Qt Python 下拉框 "currentIndexChanged" 触发两次
我有一个下拉框,它的表现有点奇怪。用户应该通过鼠标点击来选择城市的名字,下面是相关的代码:
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
两次。
这个下拉框是分层的,也就是说,首先在第一个下拉框中选择一个客户后,第二个下拉框才会显示该客户的城市列表。
我希望这里有人能指出或者猜测一下为什么会出现这个问题。
2 个回答
-1
谢谢你,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'函数里加了一个条件来避免这个问题。现在,当这个函数被'connect'错误地激活时,它会检查是否真的选择了城市名称,如果没有选择,就不会执行后面的过程,这样可以避免系统崩溃。
这是加载城市列表到下拉框的函数:
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)
这确实不是最理想的解决方案。不过,花了几个小时才发现这个奇怪的连接事件是错误地自我激活,真是有点好笑。
如果你发现了其他解释,请告诉我们。
0
我也遇到了完全一样的问题。经过一些调试后,我发现用
currentIndexChanged(int)
代替
currentIndexChanged(QString)
解决了我的问题。
不过我还是不明白为什么前者会触发两次。