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(); }
标签:补全,searchAction,auto,dynamic,输入框,new,lineEdit,completer,Qt 来源: https://www.cnblogs.com/coolbear/p/12966143.html