当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
JSP的Web监听器(Listener)
码蹄集 - MT2094 - 回文之时:第4组数据错误
重新审视分布式系统:永远不会有完美的一致性方案……
R语言使用yardstick包的gain_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的增益(gain)曲线(gain curve)
葫芦娃解析
Hubei Telecom Tianyi TY1608_S905L3B_MT7668_ card brush firmware package
15天升级打怪,成为虚拟时尚创作者
软件基础的理论
从云计算到函数计算
911S5正式谢幕后 如何找到一个好用的替代品
Selenium Webdriver驱动自管理
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
JVM内存和垃圾回收-08.方法区
谷粒商城笔记
机器学习(十一):KNN(K近邻)
御神楽的学习记录之基于FPGA的AHT10温湿度数据采集
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
HCIP笔记(6)
Analysis of the gourd baby
乐享购(分享购)的模式:优势、亮点、收益








