QT day2 (登录界面及弹出跳转)

完善登录界面

点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。

如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面

点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录

要求:对象版和静态成员函数版至少各实现一个

1、myqq.h(头文件1)

#ifndef MYQQ_H
#define MYQQ_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


namespace Ui {
class MYQQ;
}

class MYQQ : public QWidget
{
    Q_OBJECT
signals:
    void jump();            //自定义跳转信号

private slots:

    void my_jump();

    void log();


public:
    QTextToSpeech *speecher;
    explicit MYQQ(QWidget *parent = nullptr);
    ~MYQQ();
QPushButton *bin1;
QPushButton *bin2;
QPushButton *bin3;
QPushButton *bin4;
QLineEdit *edit1;
QLineEdit *edit2;


void my_slot();


void my_say();
private:
    Ui::MYQQ *ui;
};

#endif // MYQQ_H

 2、second.h(头文件2)

#ifndef SECOND_H
#define SECOND_H

#include 
#include 
#include 

namespace Ui {
class second;
}

class second : public QWidget
{
    Q_OBJECT

public:
    explicit second(QWidget *parent = nullptr);
    ~second();

public slots:
    void jump_slot();            //跳转信号对应的槽函数


private slots:

    void on_noi_clicked();

    void on_ques_clicked();

    void on_worm_clicked();

    void on_infor_clicked();

    void on_crict_clicked();

    void on_close_clicked();

private:
    Ui::second *ui;
};

#endif // SECOND_H

3、main.cpp

#include "myqq.h"
#include 
#include "second.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MYQQ w;
    w.show();

    second s;


    //将两个界面的信号与槽进行连接
    QObject::connect(&w, &MYQQ::jump, &s, &second::jump_slot);

    return a.exec();
}

4、myqq.cpp

#include "myqq.h"
#include "ui_myqq.h"
#include "second.h"
void fun()
{
    static int a= 0;
    qDebug()<setupUi(this);

    qDebug("%s","hello world");
    //qDebug() << "hello world" ;

    //设置固定尺寸
    this->setFixedSize(QSize(500,420));

    qDebug()<<"title : "<windowTitle();
    //设置窗口名称
    this->setWindowTitle("QQ");
    qDebug()<<"title : "<windowTitle();

    this->setWindowIcon(QIcon("E:/QQdownload/icon_nhgbq8i4bf/QQ.png"));
    this->setStyleSheet("background-color:white;");
    //设置透明度
    // this->setWindowOpacity(0.8);
    QLabel *lab3=new QLabel(this);
    lab3->resize(500,200);
    lab3->setScaledContents(true);
    lab3->setPixmap(QPixmap("E:/QQdownload/icon_nhgbq8i4bf/hhh.jpg"));

    //设置纯净窗口
    //this->setWindowFlags(Qt::FramelessWindowHint);

    //按钮类QPushButton

    //手动定义一个按钮,取名按钮1;
    bin1 = new QPushButton(QIcon("E:/QQdownload/icon_nhgbq8i4bf/denglu_1.png"),"登录",this);

    //重新设置大小
    bin1->resize(100,30);
    bin1->move(180,350);
    bin1->setStyleSheet("background-color:skyblue;");

    //2.手动构造一个按钮,并在构造时给定父组件
    bin2=new QPushButton(this);
    bin2->resize(bin1->size());
    bin2->setIcon(QIcon("E:/QQdownload/icon_nhgbq8i4bf/quxiao.png"));
    bin2->setText("取消");
    bin2->move(300,350);
    bin2->setStyleSheet("background-color:skyblue;");


    bin3 = new QPushButton("忘记密码",this);
    bin3->resize(100,20);
    bin3->move(330,314);

    bin3->setStyleSheet("background-color:white;""border-radius:8px;");

    bin4 = new QPushButton(QIcon("E:/QQdownload/icon_nhgbq8i4bf/QQ.png"),"注册",this);
    bin4->resize(bin1->size());
    bin4->move(10,380);
    bin4->setStyleSheet("background-color:white;""border-radius:8px;");


    //5,手动构造行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(200,35);
    edit1->move(150,220);
    edit1->setStyleSheet("background-color:white;");
    edit1->setPlaceholderText("账号");
    edit2 = new QLineEdit(this);
    edit2->move(150,270);
    edit2->resize(edit1->size());
    edit2->setEchoMode(QLineEdit::Password);
    edit2->setPlaceholderText("密码");
    edit2->setStyleSheet("background-color:white;");

    QCheckBox *box1=new QCheckBox("记住密码",this);
    box1->move(150,310);

    QCheckBox *box2=new QCheckBox("自动登录",this);
    box2->move(250,310);



    //清空文本内容
    edit2->clear();

    ///
    //7、手动构造一个标签
    QLabel *lab1=new QLabel(this);
    lab1->resize(30,35);
    lab1->move(100,220);
    lab1->setScaledContents(true);
    lab1->setPixmap(QPixmap("E:/QQdownload/icon_nhgbq8i4bf/denglu.png"));
    QLabel *lab2=new QLabel(this);
    lab2->resize(lab1->size());
    lab2->move(100,270);
    //  lab2->setStyleSheet("background-color:skyblue;");
    lab2->setScaledContents(true);
    lab2->setPixmap(QPixmap("E:/QQdownload/icon_nhgbq8i4bf/denglumima.png"));

    QPixmap pixmap("E:/QQdownload/icon_nhgbq8i4bf/qww.png"); // 加载图像
    QPixmap roundedPixmap(pixmap.size()); // 创建一个圆形图像
    roundedPixmap.fill(Qt::transparent); // 填充透明色

    QPainter painter(&roundedPixmap);
    painter.setRenderHint(QPainter::Antialiasing, true); // 设置抗锯齿
    QPainterPath path;
    path.addEllipse(roundedPixmap.rect()); // 创建圆形路径
    painter.setClipPath(path); // 设置剪裁路径
    painter.drawPixmap(pixmap.rect(), pixmap); // 在剪裁的路径内绘制原图像

    QLabel *lab4=new QLabel(this);
    lab4->resize(100,100);
    lab4->move(200,110);
    lab4->setStyleSheet("background-color:skyblue;""border-radius:50px;");
    lab4->setScaledContents(true);
    lab4->setPixmap(roundedPixmap);
    lab4->setStyleSheet("border-radius:50px;");

    //手动创建链接QT4
    //connect(bin2,SIGNAL(clicked()),this,SLOT(my_slot()));

    //手动创建链接QT5
    connect(bin2,&QPushButton::clicked,this,&MYQQ::my_slot);
    //将两个界面的信号与槽进行连接
    second s;
    QObject::connect(bin1,&QPushButton::clicked, this,&MYQQ::log);
    speecher=new QTextToSpeech(this);

    //connect(bin1,&QPushButton::clicked,fun);

}

MYQQ::~MYQQ()
{
    delete ui;
}

void MYQQ::log()
{
    QString a="123";
    if(edit1->text()==a)
    {
        if(edit2->text()==a)
        {

            QObject::connect(bin1,&QPushButton::clicked, this,&MYQQ::my_jump);
            QObject::connect(bin1,&QPushButton::clicked,this,&MYQQ::my_say);

        }
        else
        {
            //实例化一个消息对话框
            QMessageBox box(
                        QMessageBox::NoIcon,
                        "信息对话框",
                        "密码错误",
                        QMessageBox::Ok,
                        this);

            //将对象进入运行态
            //int ret =
            box.exec();
            //清空文本内容
            edit2->clear();

        }
    }
    else
    {
        //实例化一个消息对话框
        QMessageBox box(
                    QMessageBox::NoIcon,
                    "信息对话框",
                    "没有该账号",
                    QMessageBox::Ok,
                    this);

        //将对象进入运行态
        //int ret =
        box.exec();

        //清空文本内容
        edit1->clear();

        edit2->clear();
    }



}


void MYQQ::my_slot()
{
    close();
}

void MYQQ::my_say()
{
    //    const QString a="登录成功";
    //    qDebug()<setLocale(QLocale(QLocale::Chinese));
    speecher->say("登录成功");
    speecher->say("login success");
}

void MYQQ::my_jump()
{
    //关闭当前界面,并发射一个跳转信号
    emit jump();              //发射跳转信号

    this->close();           //关闭当前界面
}

5、second.cpp

#include "second.h"
#include "ui_second.h"

second::second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::second)
{
    ui->setupUi(this);
}

second::~second()
{
    delete ui;
}

void second::jump_slot()
{
    this->show();            //将该界面进行展示
}

void second::on_noi_clicked()
{
    //实例化一个消息对话框
    QMessageBox box(
                QMessageBox::NoIcon,
                "问题对话框",
                "今晚一起去KTV吧",
                QMessageBox::Ok|QMessageBox::No,
                this);

    //将对象进入运行态
    int ret = box.exec();

    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"没问题,老地方见";
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"不好意思,今晚没空,下回约";
    }
}

void second::on_ques_clicked()
{
    //实例化一个消息对话框
    QMessageBox box(
                QMessageBox::Question,
                "问题对话框",
                "今晚一起去喝酒吧",
                QMessageBox::Ok|QMessageBox::No,
                this);

    //将对象进入运行态
    int ret = box.exec();

    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"没问题,老地方见";
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"不好意思,今晚没空,下回约";
    }
}

void second::on_worm_clicked()
{
    //警告按钮对应的头文件(一次执行)
    QMessageBox::StandardButton ret = QMessageBox::warning(this,
                                                           "警告",
                                                           "有种放学别走:",
                                                           QMessageBox::Ok|QMessageBox::No,
                                                           QMessageBox::Ok
                                                           );
    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"OK,不服来干";
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"咱两谁跟谁,别开玩笑,晚上请你喝酒";
    }
}

void second::on_infor_clicked()
{
    //实例化一个消息对话框
    QMessageBox box(
                QMessageBox::Information,
                "信息对话框",
                "今天晚上有一场会议,请准时入会",
                QMessageBox::Ok|QMessageBox::No,
                this);

    //将对象进入运行态
    int ret = box.exec();

    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"好的";
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"今晚临时有事,请个假";
    }
}

void second::on_crict_clicked()
{
    //警告按钮对应的头文件(一次执行)
    QMessageBox::StandardButton ret = QMessageBox::critical(this,
                                                            "批语",
                                                            "你的稿子真烂,拿回去重新给我改",
                                                            QMessageBox::Ok|QMessageBox::No,
                                                            QMessageBox::Ok
                                                            );
    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"好的,我马上改";
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"改什么改,老子不干了";
    }
}

void second::on_close_clicked()
{
    //实例化一个消息对话框
    QMessageBox box(
                QMessageBox::Question,
                "问题对话框",
                "是否退出登录",
                QMessageBox::Ok|QMessageBox::No,
                this);

    //将对象进入运行态
    int ret = box.exec();

    if(ret == QMessageBox::Ok)
    {
        qDebug()<<"是的";
        close();
    }
    else if(ret == QMessageBox::No)
    {
        qDebug()<<"取消";
    }
}

QT day2 (登录界面及弹出跳转)_第1张图片

QT day2 (登录界面及弹出跳转)_第2张图片 

 

 

你可能感兴趣的:(QT,qt,开发语言)