.cpp文件
#include "widget.h"
#include "ui_widget.h"#include<QDebug>
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//更改窗口标题setWindowTitle("我爱xyy");//更改图标setWindowIcon(QIcon(":/icon/icon.png"));//设置提示字ui->lineEditName->setPlaceholderText("请输入名字");ui->lineEditPasswd->setPlaceholderText("请输入密码");//密码隐藏ui->lineEditPasswd->setEchoMode(QLineEdit::Password);/*单行文本框 信号演示*/connect(ui->lineEditPasswd,SIGNAL(editingFinished()),this,SLOT(lineEditPasswdEditingFinishSlotFun()));connect(ui->lineEditName,SIGNAL(textChanged(QString)),this,SLOT(lineEditUsrnameTextChangedSlotFun(QString)));connect(ui->pushButton, SIGNAL( clicked()) ,this, SLOT(login() ) );//按键ui->radioButton2->setText("登录"); //setText(QString str) 设置显示文本ui->radioButton2->setChecked(true); //setChecked(bool) 设置选中状态//按键信号connect(ui->radioButton2,SIGNAL(clicked()),this,SLOT(radioButtonChangeFun()));connect(ui->radioButton1,SIGNAL(clicked()),this,SLOT(radioButtonChangeFun()));}
void Widget::login()
{//从界面输入QString userName = ui->lineEditName->text();QString pwd = ui->lineEditPasswd->text();if(userName == "woaixyy" && pwd == "123456"){qDebug()<<"登录成功";qDebug()<<"账号 "<<ui->lineEditName->text();qDebug()<<"密码 "<<ui->lineEditPasswd->text();this->close();}else{qDebug()<<"登录失败";qDebug()<<"账号 "<<ui->lineEditName->text();qDebug()<<"密码 "<<ui->lineEditPasswd->text();ui->lineEditPasswd->clear();}
}void Widget::lineEditPasswdEditingFinishSlotFun()//表示文本确认
{qDebug()<<__func__;qDebug()<<"账号 "<<ui->lineEditName->text();qDebug()<<"密码 "<<ui->lineEditPasswd->text();
}void Widget::lineEditUsrnameTextChangedSlotFun(QString txt) // 密码的内容
{/*获取用户刚刚输入的 字符 QCharchar: 8bit asccii 256符号QChar:16bit 万国码 65536 ,在QT中,所有的字符都是QChar‘A’ =16bit;*///获取最后一个字符QChar qch = txt[ txt.length() - 1 ];if( (qch > 'z') || ( qch < 'A' )) {qDebug()<<"you input is wrong ,invaoid char "<<qch;ui->lineEditName->setText("");}qDebug()<<__func__<<" last char="<<qch;
}
void Widget::radioButtonChangeFun()
{qDebug()<<"你选择:";if(ui->radioButton2->isChecked()){qDebug()<<"登录";}if(ui->radioButton1->isChecked()){qDebug()<<"注册";}
}
Widget::~Widget()
{delete ui;
}
.h文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();
public slots:void login();void lineEditPasswdEditingFinishSlotFun();void lineEditUsrnameTextChangedSlotFun(QString);void radioButtonChangeFun();
private:Ui::Widget *ui;
};
#endif // WIDGET_H
.ui
现象