要求:
默认账户“admin”,密码“123456”
如果匹配成功,则输出登录成功并关闭界面,如果登录失败,输出登录失败,并清空密码框中的内容
点击取消按钮,则关闭界面
widget.h代码如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_loginBtn_clicked(); //系统定义的登录按钮的槽函数
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp代码如下:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//connect(ui->loginBtn,SIGNAL(clicked()),this,SLOT(on_loginBtn_clicked));
//设置窗口Icon
this->setWindowIcon(QIcon("D:/qtprojects/zls/icon/gdt.png"));
//设置窗口标题
this->setWindowTitle("高达");
//设置logo
ui->logoLab->setPixmap(QPixmap("D:/qtprojects/zls/icon/gaoda.png"));
//设置用户名和密码的icon
ui->userNameLab->resize(40,40);
ui->userNameLab->setScaledContents(true);
ui->userNameLab->setPixmap(QPixmap("D:/qtprojects/zls/icon/userName.jpg"));
ui->pwdLab->resize(40,40);
ui->pwdLab->setScaledContents(true);
ui->pwdLab->setPixmap(QPixmap("D:/qtprojects/zls/icon/passwd.jpg"));
//设置密码的行编辑器为密文模式
ui->lineEdit_2->setEchoMode(QLineEdit::Password);
//设置两个按钮的icon
ui->loginBtn->setIcon(QIcon("D:/qtprojects/zls/icon/login.png"));
ui->cancelBtn->setIcon(QIcon("D:/qtprojects/zls/icon/cancel.png"));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_loginBtn_clicked()
{
if(ui->usernameEdit->text()=="admin"&&ui->lineEdit_2->text()=="123456")
{
qDebug()<<"登陆成功";
this->close();
}
else
{
qDebug()<<"输入错误";
ui->usernameEdit->clear();
ui->lineEdit_2->clear();
}
}
mian.cpp代码如下:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
结果如图:
![](https://i-blog.csdnimg.cn/blog_migrate/62b96757ed7fc2e5e7ba1a6d00567220.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6f1416c863ce017bb09434a8034158f2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f4f74a69b44e7acd3e5ddd145b8cb8a6.png)