I subclassed QCompleter
{
Q_OBJECT
public:
{
setModel(&m_model);
}
public slots:
{
QDebug() << "update";
m_model.
setStringList(QStringList() <<
"one" <<
"two" <<
"three");
complete();
popup()->setCurrentIndex(completionModel()->index(0, 0));
}
private:
};
class mySearchFilterComplter : public QCompleter
{
Q_OBJECT
public:
mySearchFilterComplter(QObject * parent) : QCompleter(parent), m_model()
{
setModel(&m_model);
}
public slots:
void update(const QString & text)
{
QDebug() << "update";
m_model.setStringList(QStringList() << "one" << "two" << "three");
complete();
popup()->setCurrentIndex(completionModel()->index(0, 0));
}
private:
QStringListModel m_model;
};
To copy to clipboard, switch view to plain text mode
then connected combo box editTextChanged() signal to my completer update() slot.
mySearchFilterComplter * cc = new mySearchFilterComplter(this);
ui->comboBox->setComplter(cc)
connect(ui
->comboBox,
SIGNAL(editTextChanged
(QString)), cc,
SLOT(update
(QString)));
mySearchFilterComplter * cc = new mySearchFilterComplter(this);
ui->comboBox->setComplter(cc)
connect(ui->comboBox, SIGNAL(editTextChanged(QString)), cc, SLOT(update(QString)));
To copy to clipboard, switch view to plain text mode
The update() gets called with each keystroke in combo box line edit but there is no popup ?
What am I doing wrong ?
I have similar code with subsclassed QLineEdit and subclassed QCompleter and the completer popup works. (but the completer::update() is called from keyPressedEvent() though)
{
Q_OBJECT;
public:
inline MyCompleter
(const QStringList
& words,
QObject * parent
) : {
setModel(&m_model);
}
{
m_model.
setStringList(QStringList() <<
"one" <<
"two" <<
"three");
complete();
}
private:
};
{
Q_OBJECT;
public:
~MyLineEdit();
void setCompleter(MyCompleter *c);
MyCompleter *completer() const;
protected:
private slots:
void insertCompletion
(const QString &completion
);
private:
MyCompleter *c;
};
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));
}
MainWindow
::MainWindow(QWidget *parent
){
MyLineEdit * te = new MyLineEdit();
MyCompleter
* completer
= new MyCompleter
(QStringList() <<
"112233" <<
"223344" <<
"334455",
this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
te->setCompleter(completer);
layout->addWidget(te);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
}
class MyCompleter : public QCompleter
{
Q_OBJECT;
public:
inline MyCompleter(const QStringList& words, QObject * parent) :
QCompleter(parent), m_model()
{
setModel(&m_model);
}
inline void update(QString word)
{
m_model.setStringList(QStringList() << "one" << "two" << "three");
complete();
}
private:
QStringList m_list;
QStringListModel m_model;
};
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;
};
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));
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
MyLineEdit * te = new MyLineEdit();
MyCompleter * completer = new MyCompleter(QStringList() << "112233" << "223344" << "334455", this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
te->setCompleter(completer);
QVBoxLayout * layout = new QVBoxLayout;
layout->addWidget(te);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
MainWindow::~MainWindow()
{
}
To copy to clipboard, switch view to plain text mode
Bookmarks