Qt Python Combo-Box“currentIndexChanged”两次射击

问题描述 投票:0回答:2

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

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称为两次。

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

我希望这里有人可以指出或猜测为什么会这样。

python qt combobox pyqt
2个回答
0
投票

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

currentIndexChanged(INT)

代替

currentIndexChanged(QString的)

为我修好了。

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


-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.

我发现,即使没有选择城市,城市的combox-box也会被激活。是的,我已经检查过程序中的其他地方是否没有调用函数'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)

真的不是理想的解决方案。但是,非常有趣的是,必须花费数小时才能发现这种奇怪的连接事件是错误的自我激活。

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

© www.soinside.com 2019 - 2024. All rights reserved.