作业8/16

2> 在登录界面的登录取消按钮进行一下设置:

使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
//    , ui(new Ui::MainWindow)
//    , usernameInput(new QLineEdit(this))
//    , passwordInput(new QLineEdit(this))
//    , loginButton(new QPushButton("登录", this))
//    , cancelButton(new QPushButton("取消", this))
    , ui(new Ui::MainWindow)
    , usernameInput(nullptr)
    , passwordInput(nullptr)
//    , loginButton(nullptr)
//    , cancelButton(nullptr)
{
    ui->setupUi(this);
    // 设置窗口标题
       this->setWindowTitle("系统登录");

       // 设置窗口大小和固定大小
       this->resize(1897, 1067);
       this->setFixedSize(1897, 1067);

       // 设置窗口背景色
       this->setStyleSheet("background-color:white");

       // 创建动画标签
       QLabel *lab1 = new QLabel(this); // 创建QLabel实例,父类为this
       lab1->resize(1897, 1067); // 设置QLabel大小
       QMovie *movie1 = new QMovie("E:\\360MoveData\\Users\\ZYX\\Pictures\\Saved Pictures\\01.jpg"); // 创建QMovie实例,载入GIF动画
       lab1->setMovie(movie1); // 设置QLabel的动画
       movie1->start(); // 开始播放动画

       // 创建账号标签控件
       QLabel *lab2 = new QLabel(this); // 创建QLabel实例
       lab2->resize(24, 39); // 设置大小
       lab2->move(1232, 500); // 设置位置
       lab2->setPixmap(QPixmap("E:\\360MoveData\\Users\\ZYX\\Pictures\\Saved Pictures\\2.png")); // 设置标签的图片
       lab2->setScaledContents(true); // 设置图片自动缩放以适应QLabel的大小

       // 创建密码标签控件
       QLabel *lab3 = new QLabel(this); // 创建QLabel实例
       lab3->resize(24, 39); // 设置大小
       lab3->move(1232, 550); // 设置位置
       lab3->setPixmap(QPixmap("E:\\360MoveData\\Users\\ZYX\\Pictures\\Saved Pictures\\3.png")); // 设置标签的图片
       lab3->setScaledContents(true); // 设置图片自动缩放以适应QLabel的大小

//       // 创建登录按钮
//       QPushButton *btn1 = new QPushButton("登录", this); // 创建QPushButton实例
//       btn1->resize(109, 45); // 设置按钮大小
//       btn1->move(1467, 660); // 设置位置
//       btn1->setStyleSheet("background-color:white"); // 设置背景色

       // 创建登录按钮
       QPushButton *loginButton = new QPushButton("登录", this); // 创建QPushButton实例
       loginButton->resize(109, 45); // 设置按钮大小
       loginButton->move(1467, 660); // 设置位置
       loginButton->setStyleSheet("background-color:white"); // 设置背景色


       // 创建取消按钮,放在登录按钮正下方
       QPushButton *cancelButton = new QPushButton("取消", this);
       cancelButton->move(1467, 710); // 假设这是你的取消按钮的位置
       cancelButton->resize(109, 45);

       // 创建注册按钮
       QPushButton *btn2 = new QPushButton("注册", this); // 创建QPushButton实例
       btn2->resize(109, 45); // 设置按钮大小
       btn2->move(1232, 660); // 设置位置
       btn2->setEnabled(true); // 设置按钮可用

//       // 创建账号输入框
//       QLineEdit *edit1 = new QLineEdit(this); // 创建QLineEdit实例
//       edit1->resize(321, 40); // 设置大小
//       edit1->move(1256, 500); // 设置位置
//       edit1->setPlaceholderText("账号/邮箱"); // 设置提示文字
//       edit1->setEchoMode(QLineEdit::Normal); // 设置显示模式,这里更改为Normal显示正常文本,原代码设置为Password显示为密码输入模式

//       // 创建密码输入框
//       QLineEdit *edit2 = new QLineEdit(this); // 创建QLineEdit实例
//       edit2->resize(321, 40); // 设置大小
//       edit2->move(1256, 550); // 设置位置
//       edit2->setPlaceholderText("密码"); // 设置提示文字
//       edit2->setEchoMode(QLineEdit::Password); // 设置显示模式为密码输入模式

       // 创建账号输入框
       usernameInput = new QLineEdit(this); // 创建QLineEdit实例
       usernameInput->resize(321, 40); // 设置大小
       usernameInput->move(1256, 500); // 设置位置
       usernameInput->setPlaceholderText("账号/邮箱"); // 设置提示文字
       usernameInput->setEchoMode(QLineEdit::Normal); // 设置显示模式

       // 创建密码输入框
       passwordInput = new QLineEdit(this); // 创建QLineEdit实例
       passwordInput->resize(321, 40); // 设置大小
       passwordInput->move(1256, 550); // 设置位置
       passwordInput->setPlaceholderText("密码"); // 设置提示文字
       passwordInput->setEchoMode(QLineEdit::Password); // 设置显示模式

       // 使用Qt5版本信号连接登录按钮
       connect(loginButton, &QPushButton::clicked, this, &MainWindow::onLoginClicked);
       // 使用Qt4版本信号连接取消按钮
       connect(cancelButton, SIGNAL(clicked()), this, SLOT(onCancelClicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete usernameInput;
    delete passwordInput;
//    delete loginButton;
//    delete cancelButton;
}

void MainWindow::onLoginClicked()
{
    QString username = usernameInput->text();
    QString password = passwordInput->text();

    if (username == "admin" && password == "123456")
    {
        // 在应用程序输出窗口显示信息
        qDebug() << "用户已登录";

        // 关闭窗口
        this->close();
    }
    else
    {
        // 显示登录失败警告
        QMessageBox::warning(this, tr("Login"), tr("登录失败"));
        passwordInput->clear();
    }
}

void MainWindow::onCancelClicked()
{
    this->close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值