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()
{
}
相关推荐
Scott9999HH6 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社7 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海7 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖8 小时前
JDK 26 新特性详解
java·开发语言
马优晨8 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
吳所畏惧10 小时前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh
DFT计算杂谈10 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
人邮异步社区10 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大11 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai11 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang