Home > OS >  Populate QCombobox with SQL Values
Populate QCombobox with SQL Values

Time:02-23

I'm trying to populate a qcombobox with values from an SQL table, but I get

TypeError: addItems(self, Iterable[str]): argument 1 has unexpected type 'function'

My code is

self.building = QComboBox()
self.building.addItems(lambda: self.Buildingcombobox())

and

def Buildingcombobox(self):
    conn = pyodbc.connect(<connection>)
    cursor = conn.cursor()
    cursor.execute("SELECT building, building_id FROM buildings")
    rows = cursor.fetchall()

    for row in rows:
        self.building.addItem(str(row[0]), row[1])
        print(row)
    
    conn.commit()
    conn.close()

I'm selecting building and building_id because I want only building_id stored in the table employees.

CodePudding user response:

The QComboBox() object's addItems() method wants an Iterable[Str] parameter, not a function which adds items to the widget.

(For the SQL that you've quoted, you do not need to commit because you're not INSERTing, DELETEing or UPDATEing).

I would remove the call to addItems() entirely, and use your Buildcombobox function (without the commit) because the addItem() call is doing what you need.

  • Related