本章收录QLineEdit常用的方法。
常用设置
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit->setText("大南京");
ui->lineEdit->setAlignment(Qt::AlignCenter);//设置文本位置,Qt::AlignLeft左对齐,Qt::AlignCenter居中,Qt::AlignRight右对齐
ui->lineEdit->setReadOnly(true);//只读,可选中文本
ui->lineEdit->setEnabled(false);//不可编辑,不能选中文本
ui->lineEdit->setStyleSheet("color : rgb(13,118,114);font-size : 34px;font-family : '微软雅黑';");//设置字体
//输入模式,Password密码模式显示黑点,PasswordEchoOnEdit输入时正常显示,失去焦点显示为黑点,NoEcho输入时不显示,实际已输入,类型linux输入密码
ui->lineEdit_passWD->setEchoMode(QLineEdit::Password);
ui->lineEdit_passWD->setMaxLength(6);//设置最多输入的字符数
//QT自带的输入限制验证,不是特别好用,比如下面限制输入正整数时可以输入0,要严格限制可以使用正则表达式
ui->lineEdit_int->setValidator(new QIntValidator(100, 999, this));//限制输入100-999的整数
//只能输入-180到180之间的浮点数,小数点后最多两位(可用于限制经纬度等)
QDoubleValidator *pDfValidator = new QDoubleValidator(-180.0, 180.0 , 2, ui->lineEdit_double);
pDfValidator->setNotation(QDoubleValidator::StandardNotation);
ui->lineEdit_double->setValidator(pDfValidator);
ui->lineEdit_wubiankuang->setPlaceholderText("请输入姓名");//设置输入提醒
ui->lineEdit_wubiankuang->setStyleSheet("background:transparent;border-width:0;border-style:outset");//设置无边框且透明
//设置圆角,有颜色边框,输入内容起始位置后移等
ui->lineEdit_yuanjiaoYSE->setPlaceholderText("请输入学号");//设置输入提醒
ui->lineEdit_yuanjiaoYSE->setStyleSheet("border-style: outset; border-width: 2px; border-color: rgb(13,118,114);border-radius:10px;padding:2px 4px");
//QLineEdit显示图标
ui->lineEdit_zhanghao->setPlaceholderText("请输入帐号");
ui->lineEdit_zhanghao->setStyleSheet("background-image: url(./zhanghao.png);background-repeat: no-repeat;background-position: left;padding: 2 2 2 60;"
"border-style: outset; border-width: 2px; border-color: rgb(13,118,114);border-radius:10px;");
ui->lineEdit_mima->setPlaceholderText("请输入密码");
ui->lineEdit_mima->setStyleSheet("background-image: url(./mima.png);background-repeat: no-repeat;background-position: left;padding: 2 2 2 60;"
"border-style: outset; border-width: 2px; border-color: rgb(13,118,114);border-radius:10px;");
connect(ui->lineEdit_int,&QLineEdit::textChanged,this,&MainWindow::slot_lineEdit_intChanged);
}
///QLineEdit内容改变
void MainWindow::slot_lineEdit_intChanged(QString text)
{
qDebug()<<"["<<__FILE__<<"]"<<__LINE__<<__FUNCTION__<<" "<<text;
}
重写QLineEdit,实现点击变色
QLineEdit提升为QLineEdit_yjbs
qlineedit_yjbs.h
#ifndef QLINEEDIT_YJBS_H
#define QLINEEDIT_YJBS_H
#include <QLineEdit>
class QLineEdit_yjbs : public QLineEdit
{
Q_OBJECT
public:
explicit QLineEdit_yjbs(QWidget *parent = nullptr);
void installFont(QFont font);//设置字体
public slots:
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
};
#endif // QLINEEDIT_YJBS_H
qlineedit_yjbs.cpp
#include "qlineedit_yjbs.h"
QLineEdit_yjbs::QLineEdit_yjbs(QWidget *parent) : QLineEdit(parent)
{
setStyleSheet("border-style: outset; border-width: 2px; border-color: gray;color:rgb(121,121,121);border-radius:6px;padding:1px 2px;");
}
/**
* @brief 设置字体
* @param font
*/
void QLineEdit_yjbs::installFont(QFont font)
{
this->setFont(font);
}
void QLineEdit_yjbs::focusInEvent(QFocusEvent *e)
{
setStyleSheet("border-style: outset; border-width: 2px; border-color: rgb(13,118,114);color:rgb(51,51,51);border-radius:6px;padding:1px 2px;");
QLineEdit::focusInEvent(e);
}
void QLineEdit_yjbs::focusOutEvent(QFocusEvent *e)
{
setStyleSheet("border-style: outset; border-width: 2px; border-color: gray;color:rgb(121,121,121);border-radius:6px;padding:1px 2px;");
QLineEdit::focusOutEvent(e);
}