在Qt中有QCompleter这个类可以和QLineEdit组合实现自动补全功能,类似搜索框形式的,
1.QCompleter类可以在输入框输入字符时,进行提示可以匹配上的字符
例:为QLineEdit设置自动补全
QLineEdit* pLineEdit = new QLineEdit(this);
QStringList list;
list << "Eglisg" << "Chinese" << "Shanghai" << "Beijing"<<"北海"<<"北上";
QCompleter* pCompleter = new QCompleter(list, this);
pCompleter->setCaseSensitivity(Qt::CaseSensitive);
pLineEdit->setCompleter(pCompleter);
2.设置model,自动补全
QLineEdit* pLineEdit = new QLineEdit(this);
QCompleter* pCompleter = new QCompleter(this);
QDirModel* pDirModel = new QDirModel(this);
pCompleter->setModel(pDirModel);
pLineEdit->setCompleter(pCompleter);
3.一些常用设置
匹配规则设置
completer->setFilterMode(Qt::MatchStartsWith);
补全方式设置
completer->setCompletionMode(QCompleter::PopupCompletion);
显示的最大数目设置
completer->setMaxVisibleItems(5);