当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Cesium快速上手0-Cesium安装与基本介绍

智慧场馆的无人值守怎么做?

Analysis of the gourd baby

美容院管理系统有哪些促销方式?

手把手教你搭建一个Minecraft 服务器

咪咕MGV2000KL南传_S905L3B_MT7668线刷固件包

不需要服务器,教你仅用30行代码搞定实时健康码识别

Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得

移动CM101s_MV100_EMMC_M8233_强刷后全分区线刷固件包

并发编程原理学习-reentrantlock源码分析
随机推荐
SAP 电商云 Spartacus UI 页面布局的设计原理
shell脚本详解-------循环语句wuile循环和until循环
生产环境重大bug,update加上索引字段会走索引进行更新?还是走全表扫描
广东移动魔百盒M411A _905L3_线刷固件包
pygame的freetype模块
从-99打造Sentinel高可用集群限流中间件
博云入选Gartner中国云原生领域代表性厂商
Copycat CNN: Stealing Knowledge by Persuading Confession with Random Non-Labeled Data阅读心得
从正负样本解耦看对比学习为何需要large batch size训练Ddcoupled Contrastive learning (DCT)
【LeetCode每日一题】——374.猜数字大小
WPF 光标初始化的时候 temp 文件夹满了无法创建
How to convert an int attribute into a string in the json format returned by the Go language gin framework?
机器学习(十一):KNN(K近邻)
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
华为云数据治理生产线DataArts,让“数据‘慧’说话”
越来越火的图数据库到底能做什么?
HCIP WPN 实验
Mobile magic box CM201-1_CW_S905L2_MT7668_wire brush firmware package
华为应用市场“图章链接”功能上线 让APP分发突破机型壁垒
谷粒商城笔记