Search code examples
c++qtqcomboboxqtgui

QComboBox::findData() always returns -1


I am trying to get the id of the record in the model from a QCombobox with findData(index), but when select a item, it retunrs -1. It has been working in another project, but this is the second one that doesn't work. Here's my code:

modAnfi = new QSqlTableModel(this);
modAnfi->setQuery("SELECT id, (nombres || ' ' || apellidos) as Nombre, nombres, apellidos FROM tbPersonas WHERE activo=1");
comboAnfitrion->setModel(modAnfi);
comboAnfitrion->setModelColumn(1);
comboAnfitrion->setEditable(true);
comboAnfitrion->completer()->setCompletionMode(QCompleter::PopupCompletion);

connect(comboAnfitrion, SIGNAL(currentIndexChanged(int)), this, SLOT(currentIndexChangeAnfitrion(int)));

and:

void controlReg::currentIndexChangeAnfitrion(int index)
{

    qDebug() << comboAnfitrion->findData(index); // -1
    qDebug()<< comboAnfitrion->itemData(1); // QVariant(Invalid) 
}

Thanks for your time, any help will be appreciated.


Solution

  • You have to use the model you assign to the comboBox, use the index to look for it: modAnfi->data(modAnfi->index( index, 0));