views:

258

answers:

3

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.

A: 

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.

ThreaderSlash
A: 

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.

A: 

With regard to why currentIndexChanged(int) works and currentIndexChanged(QString) doesn't, I believe you would have had to use:

currentIndexChanged(const QString&)

instead.

EDIT: This comment is directed at user469276 below...

Ben