2009-12-18 77 views
7

我使用QCompleter和QLineEdit,并且我想动态更新QCompleter的模型。即模型的内容根据QLineEdit的文本更新。如何动态更新QCompleter的模型

1)mdict.h

#include <QtGui/QWidget> 

class QLineEdit; 
class QCompleter; 
class QModelIndex; 

class mdict : public QWidget 
{ 
    Q_OBJECT 

public: 
    mdict(QWidget *parent = 0); 
    ~mdict() {} 

private slots: 
    void on_textChanged(const QString &text); 

private: 
    QLineEdit *mLineEdit; 
    QCompleter *mCompleter; 
}; 

2)mdict.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); 
} 

我希望当我输入 “H”,它应该给我 “H0” 一个自动完成列表,“h1”和“h2”,我可以使用keyborad选择项目。但它不像我预期的那样。

看来应该在QLineEdit发出“textChanged”信号之前设置模型。一种方法是重新实现“keyPressEvent”,但它涉及许多条件来获取QLineEdit的文本。

所以,我想知道是否有一种简单的方法来动态更新QCompleter的模型?

+0

我已经试过事件,它的工作原理,但它是不容易的使用有这么多类型的按键(例如Backspace键......)的。在qlinecontrol.cpp中,你可以看到更多细节。我只是想知道它能以简单的方式完成吗? – 2009-12-18 15:28:57

回答

4

哦,我已经找到了答案:

使用信号 “textEdited” 而不是 “框TextChanged”。

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

+0

这对我来说也有些帮助,但是当我试图在目录中深入挖掘时仍然遇到分段错误,请查看[paste bin](http://pastebin.com/Jxziq8hW)上的代码, .. – 2013-11-04 10:38:29

1

您可以使用这样的事情:

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); 
} 
0

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

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

访问功能:

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