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 协议通过三次握手建立可靠的点对点连接,具体过程…...
React第五十七节 Router中RouterProvider使用详解及注意事项
前言 在 React Router v6.4 中,RouterProvider 是一个核心组件,用于提供基于数据路由(data routers)的新型路由方案。 它替代了传统的 <BrowserRouter>,支持更强大的数据加载和操作功能(如 loader 和…...
学校招生小程序源码介绍
基于ThinkPHPFastAdminUniApp开发的学校招生小程序源码,专为学校招生场景量身打造,功能实用且操作便捷。 从技术架构来看,ThinkPHP提供稳定可靠的后台服务,FastAdmin加速开发流程,UniApp则保障小程序在多端有良好的兼…...
ESP32 I2S音频总线学习笔记(四): INMP441采集音频并实时播放
简介 前面两期文章我们介绍了I2S的读取和写入,一个是通过INMP441麦克风模块采集音频,一个是通过PCM5102A模块播放音频,那如果我们将两者结合起来,将麦克风采集到的音频通过PCM5102A播放,是不是就可以做一个扩音器了呢…...
【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】
1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件(System Property Definition File),用于声明和管理 Bluetooth 模块相…...
Neo4j 集群管理:原理、技术与最佳实践深度解析
Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...
今日科技热点速览
🔥 今日科技热点速览 🎮 任天堂Switch 2 正式发售 任天堂新一代游戏主机 Switch 2 今日正式上线发售,主打更强图形性能与沉浸式体验,支持多模态交互,受到全球玩家热捧 。 🤖 人工智能持续突破 DeepSeek-R1&…...
今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存
文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...
稳定币的深度剖析与展望
一、引言 在当今数字化浪潮席卷全球的时代,加密货币作为一种新兴的金融现象,正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而,加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下,稳定…...
Springboot社区养老保险系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,社区养老保险系统小程序被用户普遍使用,为方…...
2025季度云服务器排行榜
在全球云服务器市场,各厂商的排名和地位并非一成不变,而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势,对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析: 一、全球“三巨头”…...
