Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Populate QCombobox with SQL Values

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading