No it doesn't, but it also does not quite do what I thought either. Run the code below, type 'B', press down-arrow twice to highlight "bison", press Enter and you get something other than row == 0.
#include <QApplication>
#include <QLineEdit>
#include <QStringList>
#include <QCompleter>
#include <QDebug>
Q_OBJECT
public:
<< "aardvark" << "bear" << "beaver" << "bison" << "civet";
setCompleter(completer);
}
private slots:
void activated(const QModelIndex& index) {
qDebug() << "Activated:" << index;
}
};
int main(int argc, char **argv) {
Test t;
t.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QLineEdit>
#include <QStringList>
#include <QCompleter>
#include <QDebug>
class Test: public QLineEdit {
Q_OBJECT
public:
Test(QWidget *p = 0): QLineEdit(p) {
QStringList const entries = QStringList()
<< "aardvark" << "bear" << "beaver" << "bison" << "civet";
QCompleter *completer = new QCompleter(entries, this);
connect(completer, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
setCompleter(completer);
}
private slots:
void activated(const QModelIndex& index) {
qDebug() << "Activated:" << index;
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
Test t;
t.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Unfortunately the row number is in the filtered list not the full model. The completer is using a proxy model internally. Unfortunately that model is undocumented/private or we might be able to use QAbstractProxyModel::mapToSource().
Here is an option:
Q_OBJECT
public:
}
}
}
signals:
void selectedSourceRow(int index);
private slots:
void generateIndexSignal(const QModelIndex& index)
{
QModelIndexList indexList = baseModel->match(
baseModel
->index
(0, completionColumn
(),
QModelIndex()),
completionRole(),
index.data(),
1,
Qt::MatchExactly);
if (!indexList.isEmpty()) {
emit selectedSourceRow(indexList.at(0).row());
}
}
};
class MyCompleter: public QCompleter {
Q_OBJECT
public:
MyCompleter(QObject *p = 0): QCompleter(p) {
connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
}
MyCompleter(QAbstractItemModel *model, QObject *p = 0): QCompleter(model, p) {
connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
}
MyCompleter(const QStringList& strings, QObject *p = 0): QCompleter(strings, p) {
connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
}
signals:
void selectedSourceRow(int index);
private slots:
void generateIndexSignal(const QModelIndex& index)
{
QAbstractItemModel * const baseModel = model();
QModelIndexList indexList = baseModel->match(
baseModel->index(0, completionColumn(), QModelIndex()),
completionRole(),
index.data(),
1,
Qt::MatchExactly);
if (!indexList.isEmpty()) {
emit selectedSourceRow(indexList.at(0).row());
}
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks