QLineEdit 常用属性

71 篇文章 4 订阅

QLineEdit
1.setText 设置文本 槽函数 不发信号
2.text()获取文本
3.setPlaceholderText 设置提示文字

QLineEdit常用属性
1.setClearButtonEnabled
2.setReadOnly
3.setMaxLength

QLineEdit撤销和恢复
1.undo()
2.redo()


#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QLineEdit *lineEdit;
    QPushButton *pushButton;
    QPushButton *pushButton_2;
    QLabel *label;
    QLineEdit *deletelineEdit;
    QLineEdit *OrlineEdit;
    QLineEdit *lineEdit_2;
    QPushButton *pushButton_3;
    QPushButton *pushButton_4;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        lineEdit = new QLineEdit(Widget);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(80, 80, 113, 25));
        pushButton = new QPushButton(Widget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(240, 80, 89, 25));
        pushButton_2 = new QPushButton(Widget);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(240, 20, 89, 25));
        label = new QLabel(Widget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(80, 20, 121, 21));
        
        deletelineEdit = new QLineEdit(Widget);
        deletelineEdit->setObjectName(QString::fromUtf8("deletelineEdit"));
        deletelineEdit->setGeometry(QRect(80, 140, 113, 25));
        deletelineEdit->setClearButtonEnabled(true);
        
        OrlineEdit = new QLineEdit(Widget);
        OrlineEdit->setObjectName(QString::fromUtf8("OrlineEdit"));
        OrlineEdit->setGeometry(QRect(80, 200, 113, 25));
        OrlineEdit->setReadOnly(true);
        
        lineEdit_2 = new QLineEdit(Widget);
        lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
        lineEdit_2->setGeometry(QRect(80, 260, 113, 25));
        lineEdit_2->setMaxLength(3);
        pushButton_3 = new QPushButton(Widget);
        pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
        
        pushButton_3->setGeometry(QRect(240, 200, 89, 25));
        pushButton_4 = new QPushButton(Widget);
        pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
        pushButton_4->setGeometry(QRect(240, 260, 89, 25));

        retranslateUi(Widget);
        QObject::connect(pushButton, SIGNAL(clicked()), Widget, SLOT(Test()));
        QObject::connect(pushButton_2, SIGNAL(clicked()), Widget, SLOT(View()));
        QObject::connect(pushButton_3, &QPushButton::clicked, lineEdit, qOverload<>(&QLineEdit::undo));
        QObject::connect(pushButton_4, &QPushButton::clicked, lineEdit, qOverload<>(&QLineEdit::redo));

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        lineEdit->setPlaceholderText(QCoreApplication::translate("Widget", "00000", nullptr));
        pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
        pushButton_2->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
        label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
        deletelineEdit->setPlaceholderText(QCoreApplication::translate("Widget", "\345\210\240\351\231\244", nullptr));
        OrlineEdit->setText(QCoreApplication::translate("Widget", "\345\217\252\350\257\273", nullptr));
        OrlineEdit->setPlaceholderText(QString());
        lineEdit_2->setPlaceholderText(QCoreApplication::translate("Widget", "\346\234\200\345\244\232\344\270\211\344\275\215", nullptr));
        pushButton_3->setText(QCoreApplication::translate("Widget", "\346\222\244\351\224\200", nullptr));
        pushButton_4->setText(QCoreApplication::translate("Widget", "\351\207\215\345\201\232", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H


QLineEdit输入验证
1.格式掩码
2.格式校验(整数,浮点数,正则表达式)

setInputMask
1.setInputMask(000.000.000.000;_)

QValidator
1.QIntValidator *ival = new QIntValidor();
2.ival->setRange(10,1000000);
3.ui.iedit->setValidator(ival);
4.QDoubleValidator *dval = new QDoubleValidator();
5.dval->setRange(10,1000000000);
6.dval->setDecimals(3);
7.ui.dedit->setValidator(dval);

QRegExpValidator
1.QRegExp rx("[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\.[a-zA-Z0-9-_]+");
2.QRegExpValidator *pReg = new QRegExpValidator(rx,this);
3.ui.email->setValidator(pReg);

QLineEdit信号

1.editingFinished 按下回车,焦点移开
2.returnPressed 格式有效
3.textChanged setText()也激发
4.textEdited

QLineEdit设置显示模式(密码)
1.Normal,NoEcho,Password,PasswordEchoOnEdit
2.setEchoMode(QLineEdit::Normal);

QLineEdit样式表-特殊符号编码
1.
QLineEdit{
border:2px dot-dash rgb(255,0,0);
border-radius:10px;
background:yellow;
selection-background-color:rgb(255,100,0);
}

QLineEdit[echoMode=“2”]{
lineedit-password-character:9731;
}

QLineEdit:read-only{
background:lightblue;
}

widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    void Test();
    void View();
    void Save();
    void Edit();
    void Return();
    void Edited(QString str);
    void Change(QString str);
private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include <QValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit->setPlaceholderText(QString::fromLocal8Bit("提示输入"));
    ui->lineEdit->setInputMask("000.000.000.000;_");
    ui->testEdit->setInputMask("AAAAA;_");
    QIntValidator *ival = new QIntValidator();
    ival->setRange(0,1000);
    ui->iEdit->setValidator(ival);

    QDoubleValidator *dval = new QDoubleValidator();
    dval->setRange(0,10000,3);
    //不用科学技术法
    dval->setNotation(QDoubleValidator::StandardNotation);
    ui->dEdit->setValidator(dval);

    //邮箱 dsdsa@dsa.com
    QRegularExpression exp("[a-zA-Z0-9-_]+@[a-zA-Z0-9-_]+\\.[a-zA-Z]+");
    QRegularExpressionValidator *rval = new QRegularExpressionValidator(exp);
    ui->email->setValidator(rval);

}

void Widget::Test()
{
    ui->lineEdit->setText("lineEdit text");
    ui->sEdit->setText("test32321");                        
}

void Widget::View()
{
    QString txt = ui->lineEdit->text();
    txt += " ";
    txt += ui->testEdit->text();
    ui->label->setText(txt);
}

void Widget::Save()
{
    ui->label->setText(ui->email->text());
    //格式不正确提示
    const QValidator *v = ui->email->validator();
    int pos = 0;
    /*
        返回
        enum State
        {
            Invalid, //不正确
            Intermediate, //中间输入
            Acceptable //格式正确
        }
    */
    QString text =  ui->email->text();
    if(v->validate(text,pos) != QValidator::Acceptable)
    {
        ui->label->setText(QString::fromLocal8Bit("格式不正确"));
    }
}

void Widget::Edit()
{
    ui->slabel->setText(ui->sEdit->text());
}

void Widget::Return()
{
    ui->rlabel->setText(ui->sEdit->text());
}

void Widget::Edited(QString str)
{
    ui->rlabel->setText(str);
}

void Widget::Change(QString str)
{
    ui->slabel->setText(str);
}

Widget::~Widget()
{
    delete ui;
}

ui_widget.h


#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
    QLineEdit *lineEdit;
    QPushButton *pushButton;
    QPushButton *pushButton_2;
    QLabel *label;
    QLineEdit *deletelineEdit;
    QLineEdit *OrlineEdit;
    QLineEdit *lineEdit_2;
    QPushButton *pushButton_3;
    QPushButton *pushButton_4;
    QLineEdit *ip;
    QLineEdit *testEdit;
    QLineEdit *iEdit;
    QLineEdit *dEdit;
    QLineEdit *email;
    QPushButton *SaveButton;
    QLineEdit *sEdit;
    QLabel *slabel;
    QLabel *rlabel;
    QLineEdit *pwlineEdit;

    void setupUi(QWidget *Widget)
    {
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QString::fromUtf8("Widget"));
        Widget->resize(800, 600);
        lineEdit = new QLineEdit(Widget);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(80, 80, 113, 25));
        pushButton = new QPushButton(Widget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(240, 80, 89, 25));
        pushButton_2 = new QPushButton(Widget);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(240, 20, 89, 25));
        label = new QLabel(Widget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setGeometry(QRect(20, 20, 201, 21));
        deletelineEdit = new QLineEdit(Widget);
        deletelineEdit->setObjectName(QString::fromUtf8("deletelineEdit"));
        deletelineEdit->setGeometry(QRect(80, 200, 113, 25));
        deletelineEdit->setClearButtonEnabled(true);
        OrlineEdit = new QLineEdit(Widget);
        OrlineEdit->setObjectName(QString::fromUtf8("OrlineEdit"));
        OrlineEdit->setGeometry(QRect(80, 260, 113, 25));
        OrlineEdit->setReadOnly(true);
        lineEdit_2 = new QLineEdit(Widget);
        lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
        lineEdit_2->setGeometry(QRect(80, 320, 113, 25));
        lineEdit_2->setMaxLength(3);
        pushButton_3 = new QPushButton(Widget);
        pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
        pushButton_3->setGeometry(QRect(240, 260, 89, 25));
        pushButton_4 = new QPushButton(Widget);
        pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
        pushButton_4->setGeometry(QRect(240, 320, 89, 25));
        ip = new QLineEdit(Widget);
        ip->setObjectName(QString::fromUtf8("ip"));
        ip->setGeometry(QRect(80, 380, 113, 25));
        testEdit = new QLineEdit(Widget);
        testEdit->setObjectName(QString::fromUtf8("testEdit"));
        testEdit->setGeometry(QRect(80, 140, 113, 25));
        iEdit = new QLineEdit(Widget);
        iEdit->setObjectName(QString::fromUtf8("iEdit"));
        iEdit->setGeometry(QRect(420, 60, 113, 25));
        dEdit = new QLineEdit(Widget);
        dEdit->setObjectName(QString::fromUtf8("dEdit"));
        dEdit->setGeometry(QRect(420, 120, 201, 25));
        email = new QLineEdit(Widget);
        email->setObjectName(QString::fromUtf8("email"));
        email->setGeometry(QRect(420, 160, 161, 25));
        SaveButton = new QPushButton(Widget);
        SaveButton->setObjectName(QString::fromUtf8("SaveButton"));
        SaveButton->setGeometry(QRect(600, 160, 89, 25));
        sEdit = new QLineEdit(Widget);
        sEdit->setObjectName(QString::fromUtf8("sEdit"));
        sEdit->setGeometry(QRect(440, 240, 113, 25));
        slabel = new QLabel(Widget);
        slabel->setObjectName(QString::fromUtf8("slabel"));
        slabel->setGeometry(QRect(580, 240, 121, 17));
        rlabel = new QLabel(Widget);
        rlabel->setObjectName(QString::fromUtf8("rlabel"));
        rlabel->setGeometry(QRect(580, 280, 67, 17));
        pwlineEdit = new QLineEdit(Widget);
        pwlineEdit->setObjectName(QString::fromUtf8("pwlineEdit"));
        pwlineEdit->setGeometry(QRect(420, 320, 281, 61));
        pwlineEdit->setStyleSheet(QString::fromUtf8("QLineEdit{ \n"
"   border:3px dotted;\n"
"   border-radius:15px;\n"
"   border-color: rgba(78, 154, 6,255);\n"
"}\n"
"\n"
"QLineEdit[echoMode=\"2\"]{\n"
"   lineedit-password-character: 9679;\n"
"   selection-background-color:rgb(255,0,0)\n"
"}"));
        pwlineEdit->setEchoMode(QLineEdit::Password);

        retranslateUi(Widget);
        QObject::connect(pushButton, SIGNAL(clicked()), Widget, SLOT(Test()));
        QObject::connect(pushButton_2, SIGNAL(clicked()), Widget, SLOT(View()));
        QObject::connect(pushButton_3, &QPushButton::clicked, lineEdit, qOverload<>(&QLineEdit::undo));
        QObject::connect(pushButton_4, &QPushButton::clicked, lineEdit, qOverload<>(&QLineEdit::redo));
        QObject::connect(lineEdit, SIGNAL(returnPressed()), Widget, SLOT(View()));
        QObject::connect(testEdit, SIGNAL(returnPressed()), Widget, SLOT(View()));
        QObject::connect(SaveButton, SIGNAL(clicked()), Widget, SLOT(Save()));
        QObject::connect(sEdit, SIGNAL(editingFinished()), Widget, SLOT(Edit()));
        QObject::connect(sEdit, SIGNAL(returnPressed()), Widget, SLOT(Return()));
        QObject::connect(sEdit, SIGNAL(textEdited(QString)), Widget, SLOT(Edited(QString)));
        QObject::connect(sEdit, SIGNAL(textChanged(QString)), Widget, SLOT(Change(QString)));

        QMetaObject::connectSlotsByName(Widget);
    } // setupUi

    void retranslateUi(QWidget *Widget)
    {
        Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
        lineEdit->setPlaceholderText(QCoreApplication::translate("Widget", "ip", nullptr));
        pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
        pushButton_2->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
        label->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
        deletelineEdit->setPlaceholderText(QCoreApplication::translate("Widget", "\345\210\240\351\231\244", nullptr));
        OrlineEdit->setText(QCoreApplication::translate("Widget", "\345\217\252\350\257\273", nullptr));
        OrlineEdit->setPlaceholderText(QString());
        lineEdit_2->setPlaceholderText(QCoreApplication::translate("Widget", "\346\234\200\345\244\232\344\270\211\344\275\215", nullptr));
        pushButton_3->setText(QCoreApplication::translate("Widget", "\346\222\244\351\224\200", nullptr));
        pushButton_4->setText(QCoreApplication::translate("Widget", "\351\207\215\345\201\232", nullptr));
        ip->setPlaceholderText(QCoreApplication::translate("Widget", "ip\350\276\223\345\205\245", nullptr));
        testEdit->setPlaceholderText(QCoreApplication::translate("Widget", "AAAAA", nullptr));
        iEdit->setPlaceholderText(QCoreApplication::translate("Widget", "\345\217\252\350\203\275\350\276\223\345\205\245\346\225\264\346\225\260", nullptr));
        dEdit->setPlaceholderText(QCoreApplication::translate("Widget", "\345\217\252\350\203\275\350\276\223\345\205\245\346\265\256\347\202\271 12.321", nullptr));
        email->setPlaceholderText(QCoreApplication::translate("Widget", "email", nullptr));
        SaveButton->setText(QCoreApplication::translate("Widget", "Save", nullptr));
        slabel->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
        rlabel->setText(QCoreApplication::translate("Widget", "TextLabel", nullptr));
        pwlineEdit->setText(QCoreApplication::translate("Widget", "dsdsadsadsa", nullptr));
        pwlineEdit->setPlaceholderText(QCoreApplication::translate("Widget", "password", nullptr));
    } // retranslateUi

};

namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值