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

//输入框添加自动搜索按钮和自动补全控件
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();
}

 

posted @ 2020-05-26 15:58  酷熊  阅读(1035)  评论(0编辑  收藏  举报