Using this QCompleter example and this custom QCompleter example, I implemented what you want.
Header:
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include <QLineEdit>
#include <QStringList>
#include <QStringListModel>
#include <QString>
#include <QCompleter>
{
Q_OBJECT
public:
inline MyCompleter
(const QStringList
& words,
QObject * parent
) : {
setModel(&m_model);
}
{
// Do any filtering you like.
// Here we just include all items that contain word.
QStringList filtered
= m_list.
filter(word, caseSensitivity
());
m_model.setStringList(filtered);
m_word = word;
complete();
}
{
return m_word;
}
private:
};
{
Q_OBJECT
public:
~MyLineEdit();
void setCompleter(MyCompleter *c);
MyCompleter *completer() const;
protected:
private slots:
void insertCompletion
(const QString &completion
);
private:
MyCompleter *c;
};
#endif // LINEEDIT_H
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include <QLineEdit>
#include <QStringList>
#include <QStringListModel>
#include <QString>
#include <QCompleter>
class MyCompleter : public QCompleter
{
Q_OBJECT
public:
inline MyCompleter(const QStringList& words, QObject * parent) :
QCompleter(parent), m_list(words), m_model()
{
setModel(&m_model);
}
inline void update(QString word)
{
// Do any filtering you like.
// Here we just include all items that contain word.
QStringList filtered = m_list.filter(word, caseSensitivity());
m_model.setStringList(filtered);
m_word = word;
complete();
}
inline QString word()
{
return m_word;
}
private:
QStringList m_list;
QStringListModel m_model;
QString m_word;
};
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent = 0);
~MyLineEdit();
void setCompleter(MyCompleter *c);
MyCompleter *completer() const;
protected:
void keyPressEvent(QKeyEvent *e);
private slots:
void insertCompletion(const QString &completion);
private:
MyCompleter *c;
};
#endif // LINEEDIT_H
To copy to clipboard, switch view to plain text mode
CPP:
MyLineEdit
::MyLineEdit(QWidget *parent
){
}
MyLineEdit::~MyLineEdit()
{
}
void MyLineEdit::setCompleter(MyCompleter *completer)
{
if (c)
QObject::disconnect(c,
0,
this,
0);
c = completer;
if (!c)
return;
c->setWidget(this);
connect(completer, SIGNAL(activated(const QString&)), this, SLOT(insertCompletion(const QString&)));
}
MyCompleter *MyLineEdit::completer() const
{
return c;
}
void MyLineEdit::insertCompletion(const QString& completion)
{
setText(completion);
selectAll();
}
{
if (c && c->popup()->isVisible())
{
// The following keys are forwarded by the completer to the widget
switch (e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // Let the completer do default behavior
}
}
bool isShortcut = (e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E;
if (!isShortcut)
QLineEdit::keyPressEvent(e
);
// Don't send the shortcut (CTRL-E) to the text edit.
if (!c)
return;
bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!isShortcut && !ctrlOrShift && e->modifiers() != Qt::NoModifier)
{
c->popup()->hide();
return;
}
c->update(text());
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}
MyLineEdit::MyLineEdit(QWidget *parent)
: QLineEdit(parent), c(0)
{
}
MyLineEdit::~MyLineEdit()
{
}
void MyLineEdit::setCompleter(MyCompleter *completer)
{
if (c)
QObject::disconnect(c, 0, this, 0);
c = completer;
if (!c)
return;
c->setWidget(this);
connect(completer, SIGNAL(activated(const QString&)), this, SLOT(insertCompletion(const QString&)));
}
MyCompleter *MyLineEdit::completer() const
{
return c;
}
void MyLineEdit::insertCompletion(const QString& completion)
{
setText(completion);
selectAll();
}
void MyLineEdit::keyPressEvent(QKeyEvent *e)
{
if (c && c->popup()->isVisible())
{
// The following keys are forwarded by the completer to the widget
switch (e->key())
{
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // Let the completer do default behavior
}
}
bool isShortcut = (e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E;
if (!isShortcut)
QLineEdit::keyPressEvent(e); // Don't send the shortcut (CTRL-E) to the text edit.
if (!c)
return;
bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!isShortcut && !ctrlOrShift && e->modifiers() != Qt::NoModifier)
{
c->popup()->hide();
return;
}
c->update(text());
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
}
To copy to clipboard, switch view to plain text mode
Usage:
MyLineEdit * te = new MyLineEdit;
MyCompleter
* completer
= new MyCompleter
(QStringList() <<
"oneone" <<
"Twotwo",
this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
te->setCompleter(completer);
MyLineEdit * te = new MyLineEdit;
MyCompleter * completer = new MyCompleter(QStringList() << "oneone" << "Twotwo", this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
te->setCompleter(completer);
To copy to clipboard, switch view to plain text mode
Bookmarks