pythonindexingpyqtqcombobox

QComboBox returns None Type . How to get Current Data?


I've implemented 2 QComboBoxes with one having items manually inserted every time and another one having items inserted with a list (I'm using Python )

But when i attempt to get the current value of Combobox , it returns None . I proceeded as specified in this question : I referred this i have provided wat i've coded ."command" and "option" are QComboBoxes ( Pardon me for bad style) Is there any mistake in Indexes ?

    self.command.insertItem(1,'Convert')
    self.command.insertItem(2,'Compose')
    self.command.insertItem(3,'Animate')


    self.option.insertItems(268,list)

and retrieval :
self.selected_com=self.command.itemData(self.command.currentIndex()) self.selected_opt=self.option.itemData(self.option.currentIndex())


Solution

  • You haven't selected a current item in the combo boxes. If there is no current item and insert a new item with insertItem, it's not selected automatically as the current one. You probably want to call self.command.setCurrentIndex(1) and self.option.setCurrentIndex(1).

    From the documentation about currentIndex():

    By default, for an empty combo box or a combo box in which no current item is set, this property has a value of -1.

    From the documentation about itemData():

    Returns the data for the given role in the given index in the combobox, or QVariant::Invalid if there is no data for this role.

    From the documentation of PyQt:

    Any Python object may be used whenever a QVariant is expected. None will be interpreted as an invalid QVariant.