#include "widget.h"
#include <QLineEdit>
#include <QVBoxLayout>
#include <QString>
#include <QAction>
#include <QRegExp>
#include <QIntValidator>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QLineEdit *pNormalLineEdit = new QLineEdit(this);
QLineEdit *pNoEchoLineEdit = new QLineEdit(this);
QLineEdit *pPasswordLineEdit = new QLineEdit(this);
QLineEdit *pPasswordEchoOnEditLineEdit = new QLineEdit(this);
pNormalLineEdit->setPlaceholderText("Normal");
pNoEchoLineEdit->setPlaceholderText("NoEcho");
pPasswordLineEdit->setPlaceholderText("Password");
pPasswordEchoOnEditLineEdit->setPlaceholderText("PasswordEchoOnEditLineEdit");
// 设置显示效果
pNormalLineEdit->setEchoMode(QLineEdit::Normal);
pNoEchoLineEdit->setEchoMode(QLineEdit::NoEcho);
pPasswordLineEdit->setEchoMode(QLineEdit::Password);
pPasswordEchoOnEditLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
//*************************************
m_pLineEdit = new QLineEdit(this);
m_pLabel = new QLabel(this);
m_pLineEdit->setPlaceholderText(QString::fromLocal8Bit("请输入搜索内容"));
m_pLabel->setText(QString::fromLocal8Bit("输入内容为:"));
QAction *pLeadingAction = new QAction(this);
pLeadingAction->setIcon(QIcon("D:/qt/LineEdit/2.png"));
m_pLineEdit->addAction(pLeadingAction, QLineEdit::LeadingPosition);
QAction *pTrailingAction = new QAction(this);
pTrailingAction->setIcon(QIcon("D:/qt/LineEdit/1.ico"));
m_pLineEdit->addAction(pTrailingAction, QLineEdit::TrailingPosition);
// 连接信号和槽
connect(pTrailingAction, SIGNAL(triggered(bool)), this, SLOT(onSearch(bool)));
//**************************************
//**************************************
QLineEdit *pIntLineEdit = new QLineEdit(this);
QLineEdit *pDoubleLineEdit = new QLineEdit(this);
QLineEdit *pValidatorLineEdit = new QLineEdit(this);
pIntLineEdit->setPlaceholderText(QString::fromLocal8Bit("整形"));
pDoubleLineEdit->setPlaceholderText(QString::fromLocal8Bit("浮点型"));
pValidatorLineEdit->setPlaceholderText(QString::fromLocal8Bit("字母和数字"));
// 整形 范围:[1, 99]
QIntValidator *pIntValidator = new QIntValidator(this);
pIntValidator->setRange(1, 99);
// 浮点型 范围:[-360, 360] 精度:小数点后2位
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
pDoubleValidator->setRange(-360, 360);
pDoubleValidator->setNotation(QDoubleValidator::StandardNotation);
pDoubleValidator->setDecimals(2);
// 字符和数字
QRegExp reg("[a-zA-Z0-9]+$");
QRegExpValidator *pValidator = new QRegExpValidator(this);
pValidator->setRegExp(reg);
pIntLineEdit->setValidator(pIntValidator);
pDoubleLineEdit->setValidator(pDoubleValidator);
pValidatorLineEdit->setValidator(pValidator);
//************************************
//************************************
QLineEdit *pIPLineEdit = new QLineEdit(this);
QLineEdit *pMACLineEdit = new QLineEdit(this);
QLineEdit *pDateLineEdit = new QLineEdit(this);
QLineEdit *pLicenseLineEdit = new QLineEdit(this);
pIPLineEdit->setInputMask("000.000.000.000;_");
pMACLineEdit->setInputMask("HH:HH:HH:HH:HH:HH;_");
pDateLineEdit->setInputMask("0000-00-00");
pLicenseLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
//************************************
QVBoxLayout *gLayout = new QVBoxLayout(this);
gLayout->addWidget(pNormalLineEdit,0,0);
gLayout->addWidget(pNoEchoLineEdit,1,0);
gLayout->addWidget(pPasswordLineEdit,2,0);
gLayout->addWidget(pPasswordEchoOnEditLineEdit,3,0);
gLayout->addWidget(m_pLineEdit,4,0);
gLayout->addWidget(m_pLabel,5,0);
gLayout->addWidget(pIntLineEdit,6,0);
gLayout->addWidget(pDoubleLineEdit,7,0);
gLayout->addWidget(pValidatorLineEdit,8,0);
gLayout->addWidget(pIPLineEdit,9,0);
gLayout->addWidget(pMACLineEdit,10,0);
gLayout->addWidget(pDateLineEdit,11,0);
gLayout->addWidget(pLicenseLineEdit,12,0);
this->setLayout(gLayout);
}
Widget::~Widget()
{
}
void Widget::onSearch(bool checked)
{
QString strText = m_pLineEdit->text();
m_pLabel->setText(QString::fromLocal8Bit("输入内容为:%1").arg(strText));
}