Qt --- Day02

实现效果:

点击登录,检验用户密码是否正确,正确则弹出消息框,点击ok转到另一个页面

不正确跳出错误消息框,默认选线为Cancel,点击Yes继续登录

点击Cancel跳出问题消息框,默认选项No,单击yes,退出程序,单击No继续登录

以下是实现的部分代码

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>  //基类QWidget的头文件
#include<QDebug>   //信息调试类,用于输出数据使用
#include<QIcon>
#include<QPushButton>
#include<QLabel>
#include<QLineEdit>
#include<QPixmap>
#include"enter_widget.h"
#include"communication.h"
#include<QMessageBox>
class Widget : public QWidget
{
    Q_OBJECT   //QT的信号与槽元对象

public:
    Widget(QWidget *parent = nullptr);//默认参数构造函数声明
    ~Widget();//虚析构声明
private:
    QLabel *pic;

    QLabel *tx;

    QPushButton* enter;
    QPushButton* login;
    QPushButton* question;

    QLabel* user;
    QLabel* passwd;

    QLineEdit* user_l;
    QLineEdit* passwd_l;

    enter_widget* en;

    QTimer * time;
};
#endif // WIDGET_H

widget.cpp

cpp 复制代码
#include "widget.h"
#include<QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    qDebug()<<this->size();   //获取当前姐买你的尺寸
    qDebug()<<this->width();  //获取当前组件的宽度
    qDebug()<<this->height(); //获取当前组件的高度
    this->setWindowTitle("QQ");
    //this->resize(500,400);//重新设置界面大小
    //this->resize(QSize(1000,1000));
    this->setFixedSize(500,400);//设置固定的界面大小
    //this->setMaximumSize(600,700);//最大界面大小
    //this->setMinimumSize(200,300);//最小界面大小
    this->setWindowIcon(QIcon("://msg/qq.png"));




    pic = new QLabel(this);
    pic->resize(this->width(),130);
    pic->setPixmap(QPixmap("://msg/fm.png"));
    pic->setScaledContents(true);


    tx = new QLabel(this);

    tx->resize(100,100);
    tx->move(200,60);
    tx->setPixmap(QPixmap("://msg/2.jpg"));
    tx->setScaledContents(true);


    enter = new QPushButton;
    enter->setParent(this);
    enter->setText("登录");
    enter->resize(100,50);
    enter->move(120,300);
    enter->setIcon(QIcon("://msg/enter.png"));
    enter->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");

    login = new QPushButton;
    login->setParent(this);
    login->setText("注册");
    login->resize(100,50);
    login->move(280,300);
    login->setIcon(QIcon("://msg/enter.png"));
    login->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");

//    question = new QPushButton;
//    question->setParent(this);
//    question->setText("问题");
//    question->resize(50,50);
//    question->move(390,300);
//    question->setStyleSheet("border:0px solid black;border-radius:10;background-color:#FFFFFF");

    user = new QLabel;
    user->setParent(this);
    user->setText("用户名:");
    user->resize(60,30);
    user->setAlignment(Qt::AlignCenter);
    //user->setFixedSize(50,20);
    user->move(enter->x(),enter->y()-120);

    passwd = new QLabel(this);
    passwd->setText("密 码:");
    passwd->resize(60,30);
    passwd->setAlignment(Qt::AlignCenter);
    //user->setFixedSize(50,20);
    passwd->move(enter->x(),enter->y()-60);


    user_l = new QLineEdit(this);
    user_l->resize(200,30);
    user_l->move(user->x()+60,user->y());
    user_l->setPlaceholderText("请输入用户名");
    user_l->setStyleSheet("border:0px solid black;border-radius:10;background-color:#b0bfbf");


    passwd_l= new QLineEdit(this);
    passwd_l->resize(200,30);
    passwd_l->move(passwd->x()+60,passwd->y());
    passwd_l->setPlaceholderText("请输入密码");
    passwd_l->setEchoMode(QLineEdit::Password);
    passwd_l->setStyleSheet("border:0px solid black;border-radius:10;background-color:#b0bfbf");

    //切换界面
    //会话界面
    en = new enter_widget;

    //交流界面
    com = new communication;


    connect(enter,&QPushButton::clicked,[=](){

        if(user_l->text()==QString("admin") && passwd_l->text() == "123456")
        {
            QMessageBox succbox(QMessageBox::NoIcon,"登录","登录成功",QMessageBox::Ok,enter);
            int err = succbox.exec();
            if(err == QMessageBox::Ok)
            {
                this->close();
                com->show();
            }
        }
        else
        {
            int err = QMessageBox::critical(
                        this,tr("错误"),
                        tr("账号密码不匹配,是否重新登录"),
                        QMessageBox::Yes | QMessageBox::Cancel,
                        QMessageBox::Cancel
                        );

            if(err == QMessageBox::Cancel)
            {
                int ques = QMessageBox::question(
                            this,
                            tr("问题"),
                            tr("是否退出"),
                            QMessageBox::Yes | QMessageBox::No,
                            QMessageBox::No
                            );
                if(ques == QMessageBox::Yes)
                {
                    this->close();
                }
            }
            this->user_l->clear();
            this->passwd_l->clear();
        }
    });


}
Widget::~Widget()
{
}
相关推荐
wengqidaifeng1 小时前
C++从菜鸟到强手:1.基础入门
开发语言·c++
hhb_6181 小时前
PHP 8.x 核心特性与工程化开发实践指南
开发语言·php
geovindu2 小时前
go: Flyweight Pattern
开发语言·设计模式·golang·享元模式
zhangfeng11332 小时前
多台服务器同时训练llamfactory 大语言模型 国家超算中心 Slurm 是目前全球最主流的开源、高性能计算(HPC)集群资源管理与作业调度系统
服务器·语言模型·开源
不会写DN3 小时前
其实跨域问题是后端来解决的? CORS
服务器·网络·面试·go
xyq20243 小时前
TypeScript中的String类型详解
开发语言
爱学习的小囧8 小时前
ESXi 8.0 原生支持 NVMe 固态硬盘吗?VMD 配置详解教程
linux·运维·服务器·esxi·esxi8.0
坚持就完事了9 小时前
Linux中的变量
linux·运维·服务器
小糖学代码9 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
handler019 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习