Qt 如何更新QCompleter';动态s模型

Qt 如何更新QCompleter';动态s模型,qt,model,qcompleter,Qt,Model,Qcompleter,我将QCompleter与QLineEdit一起使用,并希望动态更新QCompleter的模型。i、 e.模型内容根据QLineEdit的文本进行更新 1) mdct.h #包括 类QLineEdit; QCompleter类; QModelIndex类; 类mdct:publicqwidget { Q_对象 公众: MDCT(QWidget*父项=0); ~mdct(){} 专用插槽: 更改文本时无效(常量字符串和文本); 私人: QLineEdit*mLineEdit; QCompleter

我将
QCompleter
QLineEdit
一起使用,并希望动态更新
QCompleter
的模型。i、 e.模型内容根据
QLineEdit
的文本进行更新

1) mdct.h

#包括
类QLineEdit;
QCompleter类;
QModelIndex类;
类mdct:publicqwidget
{
Q_对象
公众:
MDCT(QWidget*父项=0);
~mdct(){}
专用插槽:
更改文本时无效(常量字符串和文本);
私人:
QLineEdit*mLineEdit;
QCompleter*mccompleter;
};
2) mdct.cpp

#include <cassert>
#include <QtGui>
#include "mdict.h"

mdict::mdict(QWidget *parent) : QWidget(parent), mLineEdit(0), mCompleter(0)
{
    mLineEdit = new QLineEdit(this);
    QPushButton *button = new QPushButton(this);
    button->setText("Lookup");

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(mLineEdit);
    layout->addWidget(button);
    setLayout(layout);

    QStringList stringList;
    stringList << "m0" << "m1" << "m2";
    QStringListModel *model = new QStringListModel(stringList);
    mCompleter = new QCompleter(model, this);
    mLineEdit->setCompleter(mCompleter);

    mLineEdit->installEventFilter(this);

    connect(mLineEdit, SIGNAL(textChanged(const QString&)),
            this, SLOT(on_textChanged(const QString&)));
}

void mdict::on_textChanged(const QString &)
{
    QStringListModel *model = (QStringListModel*)(mCompleter->model());
    QStringList stringList;
    stringList << "h0" << "h1" << "h2";
    model->setStringList(stringList);
}
#包括
#包括
#包括“mdct.h”
mdict::mdict(QWidget*parent):QWidget(parent)、MLIENDIT(0)、mCompleter(0)
{
mLineEdit=新的QLineEdit(此);
QPushButton*按钮=新的QPushButton(此按钮);
按钮->设置文本(“查找”);
QHBoxLayout*布局=新的QHBoxLayout(本);
布局->添加小部件(MLIENDIT);
布局->添加小部件(按钮);
设置布局(布局);
QStringList字符串列表;
stringList模型());
QStringList字符串列表;

stringList哦,我找到了答案:

使用信号
文本编辑
而不是
文本更改


调试QT的源代码告诉了我答案。

您可以使用以下方法:

Foo:Foo()
{
    ...
    QLineEdit* le_foodName = new QLineEdit(this);
    QCompleter* foodNameAutoComplete = new QCompleter(this);
    le_foodName->setCompleter(foodNameAutoComplete);

    updateFoodNameAutoCompleteModel();
    ...
}

// We call this function everytime you need to update completer
void Foo::updateFoodNameAutoCompleteModel()
{
    QStringListModel *model;
    model = (QStringListModel*)(foodNameAutoComplete->model());
    if(model==NULL)
        model = new QStringListModel();

    // Get Latest Data for your list here
    QStringList foodList = dataBaseManager->GetLatestFoodNameList() ;

    model->setStringList(foodList);
    foodNameAutoComplete->setModel(model);
}

使用
filterMode:Qt::MatchFlags
属性。此属性保存过滤的执行方式。如果将filterMode设置为
Qt::MatchStartsWith
,则仅显示以键入字符开头的条目。
Qt::MatchContains
将显示包含键入字符的条目,并且
Qt::MatchEndsWith
以键入字符结尾的模式。目前,仅实现这三种模式。将filterMode设置为任何其他
Qt::MatchFlag
将发出警告,并且不会执行任何操作。默认模式为
Qt::MatchStartWith

此属性是在Qt 5.2中引入的。

访问功能:

Qt::MatchFlags  filterMode() const
void    setFilterMode(Qt::MatchFlags filterMode)

我试过event,它可以工作,但不容易使用,因为有太多类型的按键(例如Backspace…).在qlinecontrol.cpp中,您可以看到更多详细信息。我只是想知道是否可以用一种简单的方式完成?这对我也有一定的帮助,但当我试图在目录中进行更深入的挖掘时,仍然会遇到分段错误,请查看。。
Qt::MatchFlags  filterMode() const
void    setFilterMode(Qt::MatchFlags filterMode)