当前位置:网站首页>Qt自动补全之QCompleter使用
Qt自动补全之QCompleter使用
2022-08-04 16:57:00 【欧特克_Glodon】
QCompleter能实现自动填充功能,方便用户输入,提升用户的体验,一般和QLineEdit与QComboBox搭配起来使用.QCompleter类提供了模型item的提示功能。
目的:搜索框常用来自动补全搜索内容。
效果:
代码如下:
#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;
}
边栏推荐
猜你喜欢
随机推荐
适配器模式
Minecraft 我的世界 .minecraft下的各个文件夹的用处
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
18数藏解析
咪咕MGV2000KL南传_S905L3B_MT7668线刷固件包
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
《分布式云最佳实践》分论坛,8月11日深圳见
湖北移动中兴B860AV2.1_S905L_线刷固件包
为什么买域名必须实名认证?这样做什么原因?
WEB 渗透之XXE&XML
北京海淀6家必胜客被暂停外卖订餐 存在食品安全问题
MySQL学习之运算符
重新审视分布式系统:永远不会有完美的一致性方案……
机器学习(十):朴素贝叶斯
Steady Development | Data and Insights on Mobile Game Players in Western Europe
华硕win11安全启动如何开启
软件基础的理论
码蹄集 - MT2165 - 小码哥的抽卡之旅1
HCIP笔记(7)








