C++&QT 作业9

C++&QT 作业9_第1张图片

 widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include 
#include 
#include 
#include 
#include 
#include 
#include "second.h"
 
QT_BEGIN_NAMESPACE
namespace Ui {class Widget;}
QT_END_NAMESPACE
 
 
 
 
class Widget : public QWidget
{
    Q_OBJECT
 
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
 
 
signals:
    void btn1_signal();
 
public slots:
    void btn1_slot();
 
    void btn2_slot();
 
private:
 
    Ui::Widget *ui;
    QPushButton *btn1;
    QPushButton *btn2;
    QLineEdit *edit1;
    QLineEdit *edit2;
    QLabel *lab1;
    QLabel *lab2;
    QLabel *lab3;
 
    Second *s1;
 
 
};
#endif

second.h


#ifndef SECOND_H
#define SECOND_H
 
#include 
#include 
 
 
namespace Ui {
class Second;
}
 
class Second : public QWidget
{
    Q_OBJECT
 
public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();
 
public slots:
    void jump_slot1();
 
private:
    Ui::Second *ui;
};
 
#endif

main.cpp


#include "widget.h"
#include "second.h"
#include 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
 
 
    return a.exec();

}

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_slot1(){
 
    this->show();

}

widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include
#include 
 
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    ,ui(new Ui::Widget)
{
    ui->setupUi(this);
    s1 = new Second;
 
 
 
    //将图形化界面的名字改成登录界面
    this->setWindowTitle("Login screen");
    //将图标改成自定义图片内容
    this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
    //设置ui界面的大小为合适的大小
    this->setFixedSize(QSize(400,300));
 
    //插入一个图标
    lab1 = new QLabel(this);
    lab1->resize(QSize(400,125));
    lab1->move(0,0);
    //内容要是一张图片
    lab1->setPixmap(QPixmap(":/icon/logo.png"));
    //设置图片填充
    lab1->setScaledContents(true);
 
    //插入两个行编辑器
    edit1 = new QLineEdit(this);
    edit1->resize(QSize(240,40));
    edit1->move(110,150);
    edit1->setPlaceholderText("账号");
    //输入密码
    edit2 = new QLineEdit(this);
    edit2->resize(QSize(240,40));
    edit2->move(edit1->x(),edit1->y()+55);
    edit2->setPlaceholderText("密码");
    //回显模式是密码模式
    edit2->setEchoMode(QLineEdit::Password);
 
 
    lab2 = new QLabel(this);
    lab2->resize(50,40);
    lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
    lab2->setScaledContents(true);
    lab2->move(edit1->x()-60,edit1->y());
 
    lab3 = new QLabel(this);
    lab3->resize(50,40);
    lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
    lab3->setScaledContents(true);
    lab3->move(edit2->x()-60,edit2->y());
 
    //两个按钮
    QPushButton *btn1 = new QPushButton("登录",this);
    QPushButton *btn2 = new QPushButton("取消",this);
    //重设尺寸
    btn1->resize(QSize(60,30));
    btn2->resize(btn1->size());
    //移动位置
    btn1->move(120,edit2->y()+55);
    btn2->move(btn1->x()+120,edit2->y()+55);
 
    btn1->setIcon(QIcon(":/icon/login.png"));
    btn2->setIcon(QIcon(":/icon/cancel.png"));
 
    //将两个按钮连到自己定义的槽函数中
 
    connect(btn1,&QPushButton::clicked,this,&Widget::btn1_slot);
    connect(btn2,&QPushButton::clicked,this,&Widget::btn2_slot);
    connect(this, &Widget::btn1_signal,s1,&Second::jump_slot1);
 
}
 
void Widget::btn1_slot(){
 
 
    QMessageBox msgbox;
 
    if(edit1->text()=="admin" && edit2->text()=="123456"){
 
        msgbox.setIcon(QMessageBox::NoIcon);
        msgbox.setWindowTitle("LOL");
        msgbox.setText("登录成功");
        msgbox.setStandardButtons(QMessageBox::Yes);
        msgbox.setDefaultButton(QMessageBox::Yes);
        int i = msgbox.exec();
 
        if(i == QMessageBox::Yes)
            this->close();
      
 
 
    }
    else{
        int ret;
        msgbox.setIcon(QMessageBox::Critical);
        msgbox.setWindowTitle("LOL");
        msgbox.setText("账号密码错误,是否重新登录?");
        msgbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgbox.setDefaultButton(QMessageBox::Ok);
 
        ret = msgbox.exec();
 
        if(ret == QMessageBox::Ok){       
            edit2->clear();
        }
        else if(ret == QMessageBox::Cancel){
 
            this->close();
 
        }
    }
}
void Widget::btn2_slot(){
 
    int ret =QMessageBox::warning(
                this,
                "LOL",
                "是否退出?",
                QMessageBox::Yes|QMessageBox::No,
                QMessageBox::Yes);
 
    if(ret == QMessageBox::No){
        this->close();
    }
}
 
Widget::~Widget()
{

}

你可能感兴趣的:(c++,qt,java)