PDA

View Full Version : QCompleter always return the first element, regardless of which one is selected



Deivid
13th January 2015, 07:03
Hi
I've been asked to fix a program that uses a QTableView with a few custom delegates to auto-complete data based on row/column. The thing is, it's using PopupCompletion and the value obtained is always the 'first' one.

If I have this example data (QStandard items)

data|text
1001|product 1
1002|product 2
1003|product 3
....

And I type in "pro", these first 3 items pop up, but upon hitting enter while having selected 1002 or 1003, I get the data as "1001", but the correct text.


This is the SetModelData

void productNameDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{

QLineEdit *LineEdit = static_cast<QLineEdit*>(editor);
QString value = LineEdit->text();

int code=LineEdit->completer()->currentIndex().data(Qt::UserRole).toInt(); //FIXME: Always returns the first element of the (filtered) list, regardless of selection)

if (index.column()==2 && index.row()>=6 && index.row() <= 25)
m->addProducto(code, index.row());
model->setData(index, value, Qt::EditRole);

}

I'm obviously new to Qt, could you help me out?