In Qt, there is a QCompleter class which provides auto-complete funtionanity. I want to use QListView to finish the same thing. In the following code, When the QListView shows, QLineEdit will lose focus. How could I keep QLineEdit's focus?
1) mdict.h:
#include <QtGui/QWidget>
class QLineEdit;
class QListView;
class QModelIndex;
class mdict : public QWidget
{
Q_OBJECT
public:
mdict(QWidget *parent = 0);
~mdict() {}
private slots:
void on_textChanged(const QString &);
void completeText(const QModelIndex &);
private:
QLineEdit *mLineEdit;
QListView *mView;
};
2) mdict.cpp
#include <cassert>
#include <QtGui>
#include "mdict.h"
mdict::mdict(QWidget *parent) : QWidget(parent), mLineEdit(0), mView(0)
{
mLineEdit = new QLineEdit(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(mLineEdit);
layout->addStretch(100);
setLayout(layout);
QStringList stringList;
stringList << "m0" << "m1" << "m2";
QStringListModel *model = new QStringListModel(stringList);
mView = new QListView(this);
mView->setModel(model);
mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
mView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mView->setSelectionBehavior(QAbstractItemView::SelectRows);
mView->setSelectionMode(QAbstractItemView::SingleSelection);
mView->setParent(0, Qt::Popup);
mView->setFocusPolicy(Qt::NoFocus);
mView->setFocusProxy(mLineEdit);
connect(mLineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(on_textChanged(const QString &)));
connect(mView, SIGNAL(activated(const QModelIndex &)),
this, SLOT(completeText(const QModelIndex &)));
connect(mView, SIGNAL(clicked(const QModelIndex &)),
this, SLOT(completeText(const QModelIndex &)));
}
void mdict::on_textChanged(const QString &text)
{
int lineEidtWidth = mLineEdit->width();
mView->setMinimumWidth(lineEidtWidth);
mView->setMaximumWidth(lineEidtWidth);
mView->setMaximumHeight(60);
QPoint p(0, mLineEdit->height());
int x = mLineEdit->mapToGlobal(p).x();
int y = mLineEdit->mapToGlobal(p).y();
mView->move(x, y);
mView->show();
}
void mdict::completeText(const QModelIndex &index)
{
mLineEdit->setText(index.data().toString());
mView->hide();
}
3) main.cpp
#include "mdict.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mdict w;
w.show();
return a.exec();
}