当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Codeforces Round #811 (Div. 3)
JSP 标准标签库(JSTL)[通俗易懂]
【Gazebo入门教程】第二讲 模型库导入与可视化机器人建模(模型编辑器)
九联_UNT400G_S905L2_(联通)_线刷固件包
Mobile zte ZXV10 B860AV2. 1 - A_S905L2_MT7668_ wire brush the firmware package
开一个羽毛球馆大概需要多少钱?大约15万左右可以搞定!
18 Data Collection Analysis
WEB 渗透之逻辑漏洞
移动百事通BesTV_R3300-L_S905L_8189_线刷固件包
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
response的contentType 几种类型
Cesium快速上手0-Cesium安装与基本介绍
机器学习(十六):主成成分分析(PCA)
WEB 渗透之XXE&XML
Mobile BesTV_R3300-L_S905L_8189_wire brush firmware package
湖北电信天邑TY1608_S905L3B_MT7668_卡刷固件包
咪咕MGV2000KL南传_S905L3B_MT7668线刷固件包
太一集团宣布全资收购火币旗下社交产品火信
最小区间覆盖
【商家联盟】云平台—异业联盟,打造线上线下商业相结合的系统









