QCompleter::currentIndex issue
-
Hi,
I set QCompleter to QLineEdit object... it works fine but I can't get the current index from QCompleter by currentIndex() because it always return 0 for row index.
PS
I call currentIndex() from QLineEdit editingFinished() slot.
@void MainWindow::on_lineEdit_find_editingFinished()
{
if (p_db.isOpen()) {
/*! \bug Set current row from the completer
*/
ui->tableView_table->setCurrentIndex(completer->currentIndex());
qDebug() << completer->currentIndex().row();
}
}@ -
Bump it :)
-
Hi,
First of all currentIndex returns index from 'completion model', which is filtered model containing "all the possible matches for the current completion prefix" (see QCompleter doc).
Let say you have list a,b,c and you type a. This way your completion model contains only one element.
Let say you have a,b,bc and you type b, then you will get model with two elements.Second thing is that editingFinished signal seems to be too late to read this info. Make completer with a,b,bc and choose bc, surprisingly you will get b in editingFinished slot either as index or string.
You could use QCompleter::activated signal to get which string/index was activated. But still, you will get index from current completion list, not from whole model.I'm wondering if 'completion model' is QAbstractProxyModel as it presents filtered content. If yes then you could use mapToSource to get index for full model. However, this would be workaround as you need to cast completion model into QAbstractProxyModel.
Best regards.
-
[quote author="Bogdan" date="1366753014"]Second thing is that editingFinished signal seems to be too late to read this info. Make completer with a,b,bc and choose bc, surprisingly you will get b in editingFinished slot either as index or string.
You could use QCompleter::activated signal to get which string/index was activated. But still, you will get index from current completion list, not from whole model.[/quote]This is absolutely correct because this will give wrong index. I've to get the index from the whole model not from the completer model. so do you know how I can get the real index (from the whole model) of the chose item by completer?
-
As I said, you could try to cast QCompleter::completionModel() into QAbstractProxyModel (assuming completion model is QAbstractProxyModel, I didn't check this) and use QAbstractProxyModel::mapToSource. This way you would get index from the whole model. However, I see casting as sign of wrong project, so I wouldn't do it myself.
Another way is to just search whole QCompleter::model() for completion string. Not very stylish but should work ;)