当前位置:网站首页>The use of QCompleter for Qt auto-completion
The use of QCompleter for Qt auto-completion
2022-08-04 17:03:00 【Autodesk_Glodon】
QCompleterAutomatic filling function can be realized,方便用户输入,提升用户的体验,一般和QLineEdit与QComboBox搭配起来使用.QCompleterThe class provides the modelitem的提示功能.
目的:The search box is often used to autocomplete searches.
效果:
代码如下:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<QCompleter>
#include<QFileSystemModel>
#include<QDirModel>
#pragma execution_character_set("utf-8")
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList itemList;
itemList << "北京市" << "河北省" << "河南省" << "test" << "HCTest";
// 下拉框添加数据
ui->comboBox->addItems(itemList);
// 设置可编辑
ui->comboBox->setEditable(true);
// 自动补全实例的构建
QCompleter *comp = new QCompleter(itemList,ui->comboBox->model());
// 设置匹配模式,只要包含就显示
comp->setFilterMode(Qt::MatchContains);
// 设置大小写区分,不区大小写
comp->setCaseSensitivity(Qt::CaseInsensitive);
// 设置向用户提供补全的方式
comp->setCompletionMode(QCompleter::PopupCompletion);
// 装载补全实例
ui->comboBox->setCompleter(comp);
QStringList word_list;
word_list<<"XiaoTu"<<"xiaomi"<<"Huawei"<<"huafei"<<"Shanghai"<<"shangshan"<<"abc";
QCompleter *completer = new QCompleter(word_list, this);
// 设置匹配模式,只要包含就显示
completer->setFilterMode(Qt::MatchContains);
// 设置大小写区分,不区大小写
completer->setCaseSensitivity(Qt::CaseInsensitive);
// 设置向用户提供补全的方式
completer->setCompletionMode(QCompleter::PopupCompletion);
ui->lineEdit->setCompleter(completer);
QCompleter *completer2 = new QCompleter(this);
completer2->setModel(new QDirModel(completer2));
ui->syslineEdit->setCompleter(completer2);
}
MainWindow::~MainWindow()
{
delete ui;
}
边栏推荐
猜你喜欢
随机推荐
Unity Apple登录接入
葫芦娃解析
华硕win11安全启动如何开启
域名哪家便宜?怎么买便宜域名?
ctfshow 萌新web1-21
最小区间覆盖
AtCoder Beginner Contest 262 部分题解
Heilongjiang Mobile New Magic Hundred Box M411A_2+8_S905L3A_wire brush firmware package
SRM供应商协同管理系统功能介绍
电气成套设备行业如何借助ERP系统,解决企业管理难题?
SAP 电商云 Spartacus UI 页面布局的设计原理
"Distributed cloud best practices" BBS, on August 11, shenzhen
消灭异步回调,还得是async-await
Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
通关剑指 Offer——剑指 Offer II 010. 和为 k 的子数组
代码重构:面向单元测试
18数藏解析
【LeetCode每日一题】——540.有序数组中的单一元素
【小程序】实现发动态功能
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...








