Qt输入框添加搜索按钮,以及自动补全内容

笑着哭i 提交于 2020-08-12 23:23:56
//输入框添加自动搜索按钮和自动补全控件
void addSerachIconAndCompleterToLineEdit(QLineEdit* lineEdit)
{
    QCompleter* completer = new QCompleter(lineEdit);
    QStringListModel* stringListModel = new QStringListModel(lineEdit);
    completer->setMode(stringListModel);
    //显示全部搜索结果
    completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion); 
     lineEdit->setCompleter(completer);
     auto searchAction = new QAction(lineEdit);
     searchAction->setIcon(QApplication::style()->standardIcon(QStyle::SP_FileDialogContentsView));
     //searchAction->setIcon(":/resource.query.png");
     lineEdit->addAction(searchAction, QlineEdit::TrailingPosition);
     connect(searchAction, SIGNAL(triggered()), this, SLOT(onSearchAction()));
}

void onSearchAction()
{
     QObject* sender = sender();
     QAction* action = dynamic_cast<QAction*>(sender);
     auto parent = action->parentWidget();
     auto lineEdit = dynamic_cast<QLineEdit*>(parent);
     auto text = lineEdit->text();
     auto completer = lineEdit->completer();
     auto model = dynamic_cast<QStringListModel*>(completer->model());
     QStingList queryResults;
     queryResults<<"AAA"<<"1234";
     model->setStringList(queryResults);
     completer->complete();
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!