Qt——QLineEdit
QLineEdit是一个单行文本编辑控件。
使用者可以通过很多函数,输入和编辑单行文本,比如撤销、恢复、剪切、粘贴以及拖放等。
通过改变QLineEdit的 echoMode() ,可以设置其属性,比如以密码的形式输入。
文本的长度可以由 maxLength() 限制,可以通过使用 validator() 或者 inputMask() 可以限制它只能输入数字。在对同一个QLineEdit的validator或者input mask进行转换时,最好先将它的validator或者input mask清除,以避免错误发生。
与QLineEdit相关的一个类是QTextEdit,它允许多行文字以及富文本编辑。
我们可以使用 setText() 或者 insert() 改变其中的文本,通过 text() 获得文本,通过 displayText() 获得显示的文本,使用 setSelection() 或者 selectAll() 选中文本,选中的文本可以通过cut()、copy()、paste()进行剪切、复制和粘贴,使用 setAlignment() 设置文本的位置。
文本改变时会发出 textChanged() 信号;如果不是由setText()造成文本的改变,那么会发出textEdit()信号;鼠标光标改变时会发出cursorPostionChanged()信号;当返回键或者回车键按下时,会发出returnPressed()信号。
当编辑结束,或者LineEdit失去了焦点,或者当返回/回车键按下时,editFinished()信号将会发出。
以上是Qt官方文档对QLineEdit的简要说明,下面根据个人经验,对一些常用的方法作说明:
1.setPlaceholderText()设置提示文字

豆瓣电影的搜索输入框,没有输入任何字符时,显示“电影、影人、影院、电视剧”这些占位文字,对用户输入作相关提示。
echoLineEdit->setPlaceholderText("电影、影人、影院、电视剧");
2.setEchoMode()设置模式

淘宝登录界面的一部分,用户名可以直接看到,密码一般都用小黑点掩盖。
switch (index) {
case 0:
//默认,输入什么即显示什么
echoLineEdit->setEchoMode(QLineEdit::Normal);
break;
case 1:
//密码,一般是用小黑点覆盖你所输入的字符
echoLineEdit->setEchoMode(QLineEdit::Password);
break;
case 2:
//编辑时输入字符显示输入内容,否则用小黑点代替
echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
break;
case 3:
//任何输入都看不见(只是看不见,不是不能输入)
echoLineEdit->setEchoMode(QLineEdit::NoEcho);
}
3.setAlignment()设置文本位置
switch (index) {
case 0:
alignmentLineEdit->setAlignment(Qt::AlignLeft);
break;
case 1:
alignmentLineEdit->setAlignment(Qt::AlignCenter);
break;
case 2:
alignmentLineEdit->setAlignment(Qt::AlignRight);
}
4.setReadOnly()设置能否编辑
switch (index) {
case 0:
accessLineEdit->setReadOnly(false);
break;
case 1:
accessLineEdit->setReadOnly(true);
}
5.setValidator()对输入进行限制
这种方式的实质是通过正则表达式限制输入的内容。

比如上面的手机号输入框,控制其不能输入英文汉字等无关字符。
switch (index) {
case 0:
//无限制
validatorLineEdit->setValidator(0);
break;
case 1:
//只能输入整数
validatorLineEdit->setValidator(new QIntValidator(
validatorLineEdit));
break;
case 2:
//实例,只能输入-180到180之间的小数,小数点后最多两位(可用于限制经纬度等)
QDoubleValidator *pDfValidator = new QDoubleValidator(-180.0, 180.0 , 2, validatorLineEdit);
pDfValidator->setNotation(QDoubleValidator::StandardNotation);
validatorLineEdit->setValidator(pDfValidator);
}
6.setInputMask()对输入进行限制
通过限制格式限制输入,具体怎么格式化可以参考Qt助手。

switch (index) {
case 0:
inputMaskLineEdit->setInputMask("");
break;
case 1:
inputMaskLineEdit->setInputMask("+99 99 99 99 99;_");
break;
case 2:
inputMaskLineEdit->setInputMask("0000-00-00");
inputMaskLineEdit->setText("00000000");
inputMaskLineEdit->setCursorPosition(0);
break;
case 3:
inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
}
7.setMaxLength()设置可以输入的最多字符数
//最多只能输入9个字符
echoLineEdit->setMaxLength(9);
8.validator和inputmask的结合
比如纬度用“度:分:秒”的格式表示,分和秒的范围都是00-59,度的范围是-89到89。
QRegExp rx("(-|\\+)?[0-8]\\d:[0-5]\\d:[0-5]\\d");
echoLineEdit->setValidator(new QRegExpValidator(rx, echoLineEdit));
echoLineEdit->setInputMask("#00:00:00;0");
echoLineEdit->setText("+00:00:00");
如果不控制输入,那么必须在输入后检查输入是否合法,但控制输入后的输入肯定是合法的,可以省去检查合法的繁琐步骤。只需使用正则表达式控制输入的度分秒范围,然后控制输入的格式。

一些测试代码供参考——
头文件:
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QComboBox;
class QLineEdit;
QT_END_NAMESPACE
//! [0]
class Window : public QWidget
{
Q_OBJECT
public:
Window();
public slots:
void echoChanged(int);
void validatorChanged(int);
void alignmentChanged(int);
void inputMaskChanged(int);
void accessChanged(int);
private:
QLineEdit *echoLineEdit;
QLineEdit *validatorLineEdit;
QLineEdit *alignmentLineEdit;
QLineEdit *inputMaskLineEdit;
QLineEdit *accessLineEdit;
};
//! [0]
#endif
实现:
#include <QtWidgets>
#include "window.h"
//! [0]
Window::Window()
{
QGroupBox *echoGroup = new QGroupBox(tr("Echo"));
QLabel *echoLabel = new QLabel(tr("Mode:"));
QComboBox *echoComboBox = new QComboBox;
echoComboBox->addItem(tr("Normal"));
echoComboBox->addItem(tr("Password"));
echoComboBox->addItem(tr("PasswordEchoOnEdit"));
echoComboBox->addItem(tr("No Echo"));
echoLineEdit = new QLineEdit;
//test
/*QRegExp rx("(-|\\+)?[0-8]\\d:[0-5]\\d:[0-5]\\d");
echoLineEdit->setValidator(new QRegExpValidator(rx, echoLineEdit));
echoLineEdit->setInputMask("#00:00:00;0");
echoLineEdit->setText("+00:00:00");*/
//echoLineEdit->setMaxLength(9);
echoLineEdit->setPlaceholderText("电影、影人、影院、电视剧");
echoLineEdit->setFocus();
//! [0]
//! [1]
QGroupBox *validatorGroup = new QGroupBox(tr("Validator"));
QLabel *validatorLabel = new QLabel(tr("Type:"));
QComboBox *validatorComboBox = new QComboBox;
validatorComboBox->addItem(tr("No validator"));
validatorComboBox->addItem(tr("Integer validator"));
validatorComboBox->addItem(tr("Double validator"));
validatorLineEdit = new QLineEdit;
validatorLineEdit->setPlaceholderText("Placeholder Text");
//! [1]
//! [2]
QGroupBox *alignmentGroup = new QGroupBox(tr("Alignment"));
QLabel *alignmentLabel = new QLabel(tr("Type:"));
QComboBox *alignmentComboBox = new QComboBox;
alignmentComboBox->addItem(tr("Left"));
alignmentComboBox->addItem(tr("Centered"));
alignmentComboBox->addItem(tr("Right"));
alignmentLineEdit = new QLineEdit;
alignmentLineEdit->setPlaceholderText("Placeholder Text");
//! [2]
//! [3]
QGroupBox *inputMaskGroup = new QGroupBox(tr("Input mask"));
QLabel *inputMaskLabel = new QLabel(tr("Type:"));
QComboBox *inputMaskComboBox = new QComboBox;
inputMaskComboBox->addItem(tr("No mask"));
inputMaskComboBox->addItem(tr("Phone number"));
inputMaskComboBox->addItem(tr("ISO date"));
inputMaskComboBox->addItem(tr("License key"));
inputMaskLineEdit = new QLineEdit;
inputMaskLineEdit->setPlaceholderText("Placeholder Text");
//! [3]
//! [4]
QGroupBox *accessGroup = new QGroupBox(tr("Access"));
QLabel *accessLabel = new QLabel(tr("Read-only:"));
QComboBox *accessComboBox = new QComboBox;
accessComboBox->addItem(tr("False"));
accessComboBox->addItem(tr("True"));
accessLineEdit = new QLineEdit;
accessLineEdit->setPlaceholderText("Placeholder Text");
//! [4]
//! [5]
connect(echoComboBox, SIGNAL(activated(int)),
this, SLOT(echoChanged(int)));
connect(validatorComboBox, SIGNAL(activated(int)),
this, SLOT(validatorChanged(int)));
connect(alignmentComboBox, SIGNAL(activated(int)),
this, SLOT(alignmentChanged(int)));
connect(inputMaskComboBox, SIGNAL(activated(int)),
this, SLOT(inputMaskChanged(int)));
connect(accessComboBox, SIGNAL(activated(int)),
this, SLOT(accessChanged(int)));
//! [5]
//! [6]
QGridLayout *echoLayout = new QGridLayout;
echoLayout->addWidget(echoLabel, 0, 0);
echoLayout->addWidget(echoComboBox, 0, 1);
echoLayout->addWidget(echoLineEdit, 1, 0, 1, 2);
echoGroup->setLayout(echoLayout);
//! [6]
//! [7]
QGridLayout *validatorLayout = new QGridLayout;
validatorLayout->addWidget(validatorLabel, 0, 0);
validatorLayout->addWidget(validatorComboBox, 0, 1);
validatorLayout->addWidget(validatorLineEdit, 1, 0, 1, 2);
validatorGroup->setLayout(validatorLayout);
QGridLayout *alignmentLayout = new QGridLayout;
alignmentLayout->addWidget(alignmentLabel, 0, 0);
alignmentLayout->addWidget(alignmentComboBox, 0, 1);
alignmentLayout->addWidget(alignmentLineEdit, 1, 0, 1, 2);
alignmentGroup-> setLayout(alignmentLayout);
QGridLayout *inputMaskLayout = new QGridLayout;
inputMaskLayout->addWidget(inputMaskLabel, 0, 0);
inputMaskLayout->addWidget(inputMaskComboBox, 0, 1);
inputMaskLayout->addWidget(inputMaskLineEdit, 1, 0, 1, 2);
inputMaskGroup->setLayout(inputMaskLayout);
QGridLayout *accessLayout = new QGridLayout;
accessLayout->addWidget(accessLabel, 0, 0);
accessLayout->addWidget(accessComboBox, 0, 1);
accessLayout->addWidget(accessLineEdit, 1, 0, 1, 2);
accessGroup->setLayout(accessLayout);
//! [7]
//! [8]
QGridLayout *layout = new QGridLayout;
layout->addWidget(echoGroup, 0, 0);
layout->addWidget(validatorGroup, 1, 0);
layout->addWidget(alignmentGroup, 2, 0);
layout->addWidget(inputMaskGroup, 0, 1);
layout->addWidget(accessGroup, 1, 1);
setLayout(layout);
setWindowTitle(tr("Line Edits"));
}
//! [8]
//! [9]
void Window::echoChanged(int index)
{
switch (index) {
case 0:
//默认,输入什么即显示什么
echoLineEdit->setEchoMode(QLineEdit::Normal);
break;
case 1:
//密码,一般是用小黑点覆盖你所输入的字符
echoLineEdit->setEchoMode(QLineEdit::Password);
break;
case 2:
//编辑时输入字符显示输入内容,否则用小黑点代替
echoLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
break;
case 3:
//任何输入都看不见(只是看不见,不是不能输入)
echoLineEdit->setEchoMode(QLineEdit::NoEcho);
}
}
//! [9]
//! [10]
void Window::validatorChanged(int index)
{
switch (index) {
case 0:
//无限制
validatorLineEdit->setValidator(0);
break;
case 1:
//只能输入整数
validatorLineEdit->setValidator(new QIntValidator(
validatorLineEdit));
break;
case 2:
//实例,只能输入-180到180之间的小数,小数点后最多两位(可用于限制经纬度等)
QDoubleValidator *pDfValidator = new QDoubleValidator(-180.0, 180.0 , 2, validatorLineEdit);
pDfValidator->setNotation(QDoubleValidator::StandardNotation);
validatorLineEdit->setValidator(pDfValidator);
}
validatorLineEdit->clear();
}
//! [10]
//! [11]
void Window::alignmentChanged(int index)
{
switch (index) {
case 0:
alignmentLineEdit->setAlignment(Qt::AlignLeft);
break;
case 1:
alignmentLineEdit->setAlignment(Qt::AlignCenter);
break;
case 2:
alignmentLineEdit->setAlignment(Qt::AlignRight);
}
}
//! [11]
//! [12]
void Window::inputMaskChanged(int index)
{
switch (index) {
case 0:
inputMaskLineEdit->setInputMask("");
break;
case 1:
inputMaskLineEdit->setInputMask("+99 99 99 99 99;_");
break;
case 2:
inputMaskLineEdit->setInputMask("0000-00-00");
inputMaskLineEdit->setText("00000000");
inputMaskLineEdit->setCursorPosition(0);
break;
case 3:
inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
}
}
//! [12]
//! [13]
void Window::accessChanged(int index)
{
switch (index) {
case 0:
accessLineEdit->setReadOnly(false);
break;
case 1:
accessLineEdit->setReadOnly(true);
}
}
//! [13]
相关文章:
Qt——QLineEdit
QLineEdit是一个单行文本编辑控件。 使用者可以通过很多函数,输入和编辑单行文本,比如撤销、恢复、剪切、粘贴以及拖放等。 通过改变QLineEdit的 echoMode() ,可以设置其属性,比如以密码的形式输入。 文本的长度可以由 maxLength(…...
前端-HTML-zxst
HTML HTML是超文本标记语言(HyperText Mark-up Language) CSS是层叠样式表(Cascading Style Sheets) JS,即JavaScript是一种具有函数优先的轻量级,解释型或即时编译型的编程语言 <!--doctype标签声明…...
终极方案,清理 docker 占用磁盘过大问题, 亲测有效!
背景 在笔者的工作测试环境中,使用过程中突然出现根磁盘快吃满了(docker也是使用的根池盘的/var/lib/docker), wtf ? 服务用不了? 当然网上找到了一些常规的清楚docker 日志文件 但是通过df -hT 查看到over…...
puzzle(1321)时间旅人
时间旅人 最强大脑同款项目。 每个指针会带动周围2圈指针一起带动,内圈8个旋转180度,外圈16个旋转90度,全部调整为朝上则胜利。 问题本质: 很明显,问题本质就是求每个格子的点击次数,最少为…...
活动预告 | 2023 Meet TVM 开年首聚,上海我们来啦!
内容一览:从去年 12 月延期至今的 TVM 线下聚会终于来了!首站地点我们选在了上海,并邀请到了 4 位讲师结合自己的工作实践,分享 TVM 相关的开发经验,期待与大家线下相聚~ 关键词:2023 Meet TVM 线下活动 自…...
CoreIDRAW 软件的强大功能及适用性
1.1 绘图功能CoreIDRAW 软件是一种特殊的设计软件和图形绘制软件,使用方便、功能强大,在网页效果、商业插画设计、海报广告设计、平面设计等各类行业中都得到广泛的应用,在服装设计行业中,也逐渐地投入使用。由于纺织服装行业在设…...
JavaScript Window History
在 Web 开发中,JavaScript Window History(浏览器窗口历史记录)是一个非常有用的对象,它提供了一个接口来与浏览器历史记录进行交互。JavaScript Window History 对象允许您访问当前会话的历史记录,以及在会话历史记录…...
2023年人力资源管理师报名和培训费用是多少
2023年考人力资源管理师各个地区的收费标准不同,报名费用在几百元左右,培训费上千,具体看各地区人力资源管理师考试报名要求。 12023人力资源管理师考试费用 人力资源管理师考试分为四个等级,各级别费用是不同的,一般来…...
2023-2-23 刷题情况
灌溉花园的最少水龙头数目 题目描述 在 x 轴上有一个一维的花园。花园长度为 n,从点 0 开始,到点 n 结束。 花园里总共有 n 1 个水龙头,分别位于 [0, 1, …, n] 。 给你一个整数 n 和一个长度为 n 1 的整数数组 ranges ,其中…...
数据归档,存储的完美储备军
数据爆炸性增长的同时,存储成为了大家首要担心的问题大家都希望自家数据保存20年、50年后仍完好无损但是,N年后的数据量已达到一个无法预测的峰值如此大量的数据在保存时极可能存在丢失、损坏等问题这时需要提前对数据进行“备份”、“归档”备份是对数据…...
ES6-11、基本全部语法
一,变量声明:let声明变量:1.变量不可重复声明,let star 罗志祥 let star 小猪结果报错2.块级作用域,{ let girl 周扬青 }在大括号内的都属于作用域内3.不存在变量提升4.不影响作用域链const声明常量:const SCHOOL …...
Spring Boot整合Thymeleaf和FreeMarker模板
虽然目前市场上多数的开发模式采用前后端分离的技术,视图层的技术在小一些的项目中还是非常有用的,所以一直也占有一席之地,如spring官方的spring.io等网站就是使用视图层技术实现的。 目前Spring Boot支持的较好的两个视图层模板引擎是Thyme…...
SQL的四种连接-左外连接、右外连接、内连接、全连接
SQL的四种连接-左外连接、右外连接、内连接、全连接 内连接inner join…on… / join…on… 展现出来的是共同的数据 select m.Province,S.Name from member m inner join ShippingArea s on m.Provinces.ShippingAreaID; 相当于:select m.Province,S.Name from m…...
“点工”的觉悟,5年时间从7K到24K的转变,我的测试道路历程~
2015年7月我从一个90%以上的人都不知道的二本院校毕业(新媒体专业),凭借自学的软件测试(点点点)在北京找到了一份月薪7000的工作,在当时其实还算不错,毕竟我的学校起点比较差,跟大部…...
【Web安全-MSF记录篇章一】
文章目录前言msfvenom生成远控木马基本系统命令webcam 摄像头命令常用的信息收集脚本注册表设置nc后门开启 rdp&添加用户获取哈希mimikatz抓取密码前言 最近打站,可以感觉到之前的学的渗透知识忘记很多。。。。。多用多看多练,简单回顾一下 msfven…...
配置Flutter开发环境
一、在Windows上搭建Flutter开发环境 1、去flutter官网下载其最新可用的安装包,下载地址:https://flutter.dev/docs/development/tools/sdk/releases 。 注意,Flutter的渠道版本一直在不断的更新,请以Flutter官网为准。 另外&…...
23年六级缓考
【【六级674】3月六级规划+许愿成功的小伙伴记得来还愿啦!!(四六级延期考2周冲刺计划)】https://www.bilibili.com/video/BV1nx4y1w7fz?vd_source=5475f4f6010a81c8e6d4789af8e1a20f 作文...
低代码选型,论协同开发的重要性
Git是一款用于分布式版本控制的免费开源软件: 它可以跟踪到所有文件集中任意的变更,通常用于在软件开发期间,协调配合程序员之间的代码程序开发工作。 Git 最初诞生的原因源于Linux 内核的开发,2005年Linus Torvalds 编写出了Git。其他内核开…...
【第二十二部分】游标
【第二十二部分】游标 文章目录【第二十二部分】游标22. 游标22.1 游标的定义22.2 游标的使用22.3 游标优缺点总结22. 游标 22.1 游标的定义 当我们筛选条件的时候,虽然可以使用WHERE或者HAVING去选出我们想要的字段,但是去无法将一大块的结果集进行遍…...
【面试题】2023高频前端面试题20题
大厂面试题分享 面试题库前端后端面试题库 (面试必备) 推荐:★★★★★地址:前端面试题库1. 简述 TCP 连接的过程(淘系)参考答案:TCP 协议通过三次握手建立可靠的点对点连接,具体过程…...
RocketMQ延迟消息机制
两种延迟消息 RocketMQ中提供了两种延迟消息机制 指定固定的延迟级别 通过在Message中设定一个MessageDelayLevel参数,对应18个预设的延迟级别指定时间点的延迟级别 通过在Message中设定一个DeliverTimeMS指定一个Long类型表示的具体时间点。到了时间点后…...
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...
rknn优化教程(二)
文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK,开始写第二篇的内容了。这篇博客主要能写一下: 如何给一些三方库按照xmake方式进行封装,供调用如何按…...
基于服务器使用 apt 安装、配置 Nginx
🧾 一、查看可安装的 Nginx 版本 首先,你可以运行以下命令查看可用版本: apt-cache madison nginx-core输出示例: nginx-core | 1.18.0-6ubuntu14.6 | http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages ng…...
自然语言处理——循环神经网络
自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元(GRU)长短期记忆神经网络(LSTM)…...
云原生玩法三问:构建自定义开发环境
云原生玩法三问:构建自定义开发环境 引言 临时运维一个古董项目,无文档,无环境,无交接人,俗称三无。 运行设备的环境老,本地环境版本高,ssh不过去。正好最近对 腾讯出品的云原生 cnb 感兴趣&…...
基于IDIG-GAN的小样本电机轴承故障诊断
目录 🔍 核心问题 一、IDIG-GAN模型原理 1. 整体架构 2. 核心创新点 (1) 梯度归一化(Gradient Normalization) (2) 判别器梯度间隙正则化(Discriminator Gradient Gap Regularization) (3) 自注意力机制(Self-Attention) 3. 完整损失函数 二…...
基于Springboot+Vue的办公管理系统
角色: 管理员、员工 技术: 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能: 该办公管理系统是一个综合性的企业内部管理平台,旨在提升企业运营效率和员工管理水…...
计算机基础知识解析:从应用到架构的全面拆解
目录 前言 1、 计算机的应用领域:无处不在的数字助手 2、 计算机的进化史:从算盘到量子计算 3、计算机的分类:不止 “台式机和笔记本” 4、计算机的组件:硬件与软件的协同 4.1 硬件:五大核心部件 4.2 软件&#…...
【java面试】微服务篇
【java面试】微服务篇 一、总体框架二、Springcloud(一)Springcloud五大组件(二)服务注册和发现1、Eureka2、Nacos (三)负载均衡1、Ribbon负载均衡流程2、Ribbon负载均衡策略3、自定义负载均衡策略4、总结 …...
