如图,直接使用qcompleter时,当匹配项出现的时候,中间无法输入汉字,第一个字母会以英文形式输入。
ui->setupUi(this);
myLineEdit *lineEdit = new myLineEdit();
ui->layout->addWidget(lineEdit);
lineEdit->installEventFilter(lineEdit);
QStringList wordList;
wordList <<"啊啊"<<"啊啊哈"<<"嘿嘿"<<"嘿哦"<<"america"<<"orion"<<"amit"<<"Odssey"<<"啊啊啊啊啊";
QCompleter *completer = new QCompleter(wordList);
completer->setCaseSensitivity(Qt::CaseInsensitive);//大小写匹配
completer->popup()->setAttribute(Qt::WA_InputMethodEnabled);
completer->setFilterMode(Qt::MatchContains);//设置QCompleter支持匹配字符搜索
completer->setCaseSensitivity(Qt::CaseInsensitive);//这个属性可设置进行匹配时的大小写敏感性
completer->setCompletionRole(Qt::UserRole); //设置ItemDataRole
completer->setWrapAround(false);
lineEdit->setCompleter(completer);
设置setCompletionRole即可。
效果: