Unable to populate QCombobox

Hello Team,
I have two combo boxes in my application but one isn’t working. I copy/pasted the part thats not working below with the error message. what am i doing wrong?

Error message received:

TypeError: arguments did not match any overloaded call:
addItem(self, text: Optional[str], userData: Any = None): argument 1 has unexpected type ‘list’
addItem(self, icon: QIcon, text: Optional[str], userData: Any = None): argument 1 has unexpected type ‘list’

import os
import sys
import openpyxl
from PyQt6.QtWidgets import (QApplication, QLabel, QWidget, QMainWindow, QComboBox, QVBoxLayout)

class Testing(QWidget):
     def __init__(self):
        super().__init__()
        self.setWindowTitle("Script testing")
        self.filePath = "projectAssessment.xlsx"        
        self.rootLayout = QVBoxLayout(self) # Parent layout            
        self.searchBy = QLabel("Learing ComboBox")
        self.rootLayout.addWidget(self.searchBy)
        self.setLayout(self.rootLayout)

        self.comboList1 = self.getComboboxListing()
        self.cbo2 = QComboBox()        
        self.cbo2.addItem(self.comboList1)
        self.rootLayout.addWidget(self.cbo2)

     def getComboboxListing(self):
        self.list1 = [] # create list
        self.workBook = openpyxl.load_workbook(self.filePath, read_only=True)
        self.sheet = self.workBook.worksheets[0] # Get the first sheet 
        for rec in self.sheet[1]:
           self.list1.append(rec.value)
           return self.list1           
#########################################################################
app = QApplication(sys.argv)
rootWindow = Testing()
rootWindow.show()
sys.exit(app.exec())

Hi @robin01, you probably need to iterate over your data, and add an item for each value separately. Also note that you’re immediately returning the list after appending the first value, so list1 will either contain 0 or 1 elements… so maybe try this instead:

class Testing(QWidget):
    def __init__(self):
        ...
        # Add values separately
        for value in self.comboList1:
            self.cbo2.addItem(value) 
        ...

    def getComboboxListing(self):
        ...
        for rec in self.sheet[1]:
            self.list1.append(rec.value)

        # Return entire list
        return self.list1 

If this doesn’t solve the problem, could you provide a sample of your excel data?

2 Likes

Also probably need to guarantee that value is a string; the type definition given in the error is expecting a string (or null) as the first non-self parameter… and value yields whatever type is in the cell (string, float, int, or DateTime).

1 Like

Hi m3g4p0p,

Thanks for the reply. 30mins after the post I found the mistake, it was a spelling mistake that caused the error message.

Before: self.cbo2.addItem(self.comboList1)
AFter: self.cbo2.addItems(self.comboList1)

I looked at it million times and never noticed it and two hours I’m never getting back. :slight_smile:

3 Likes