form.h
- #ifndef FORM_H
- #define FORM_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- namespace Ui {
- class Form;
- }
-
- class Form : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Form(QWidget *parent = nullptr);
- ~Form();
-
- private:
- Ui::Form *ui;
- public slots:
- void jump_slot();
- };
-
- #endif // FORM_H
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include "form.h"
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- private:
- Ui::MainWindow *ui;
- QLineEdit *zh;
- QLineEdit *mm;
- Form *f1;
- signals:
- void my_sig();
- void jump();
- public slots:
- void btn1_slot();
- void btn2_slot();
- };
- #endif // MAINWINDOW_H
form.cpp
- #include "form.h"
- #include "ui_form.h"
-
- Form::Form(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Form)
- {
- ui->setupUi(this);
- }
-
- Form::~Form()
- {
- delete ui;
- }
- void Form::jump_slot()
- {
- show();
- }
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- //设置固定大小
- this->setFixedSize(400,300);
- //设置窗口标题
- this->setWindowTitle("HUAQI");
- //设置窗口图标
- this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
- //构建两个按钮
- QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
- btn1->resize(100,40);
- btn1->move(100,250);
-
- QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
- btn2->resize(btn1->size());
- btn2->move(btn1->x()+150,btn1->y());
- connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));
- //构建两个行编辑器
- QLineEdit *edit1=new QLineEdit(this);
- edit1->resize(200,30);
- edit1->setEchoMode(QLineEdit::Password);
- edit1->setText("123456");
- edit1->move(125,btn1->y()-50);
- QLineEdit *edit2=new QLineEdit(this);
- edit2->resize(200,30);
- edit2->setText("admin");
- edit2->move(125,edit1->y()-50);
- //构建三个标签
- QLabel *lab1=new QLabel(this);
- lab1->resize(30,30);
- lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
- lab1->setScaledContents(true);
- lab1->move(edit1->x()-40,edit1->y());
-
- QLabel *lab2=new QLabel(this);
- lab2->resize(lab1->size());
- lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
- lab2->setScaledContents(true);
- lab2->move(edit2->x()-40,edit2->y());
-
- QLabel *lab3=new QLabel(this);
- lab3->resize(400,120);
- lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
- lab3->setScaledContents(true);
-
- connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
- zh=edit2;
- mm=edit1;
-
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
-
- void MainWindow::btn1_slot()
- {
-
- if(mm->text()=="123456"&&zh->text()=="admin")
- {
- QMessageBox box(QMessageBox::NoIcon,
- "成功",
- "登陆成功",
- QMessageBox::Ok,
- this);
- int ret=box.exec();
- if(ret==QMessageBox::Ok)
- {
- emit jump();
- }
- //静态
- //QMessageBox box(this,
- // "成功",
- // "登陆成功",
- // QMessageBox::Ok,
- // QMessageBox::Ok);
- // int ret=box.exec();
- // if(ret==QMessageBox::Ok)
- // {
- //
- // emit jump();
- // }
- }else
- {
- QMessageBox box(QMessageBox::Warning,
- "错误",
- "账号或密码出错",
- QMessageBox::Yes|QMessageBox::No,
- this);
-
- box.setButtonText(QMessageBox::Yes,"继续");
- box.setButtonText(QMessageBox::No,"取消");
-
- int ret=box.exec();
- if(ret==QMessageBox::Yes)
- {
-
- }else
- {
- this->close();
- }
- //静态
- //int ret=QMessageBox::warning(this,
- // "错误",
- // "账号或密码出错",
- // QMessageBox::Yes|QMessageBox::No,
- // QMessageBox::No);
- // if(ret==QMessageBox::Yes)
- // {
-
- // }else
- // {
- // this->close();
- // }
- }
-
- }
-
- void MainWindow::btn2_slot()
- {
- QMessageBox box(QMessageBox::Question,
- "退出",
- "确定要退出吗?",
- QMessageBox::Yes|QMessageBox::No,
- this);
- int ret=box.exec();
- if(ret==QMessageBox::Yes)
- {
- this->close();
- }
- //静态
- //int ret=QMessageBox::warning(this,
- // "退出",
- // "确定要退出吗?",
- // QMessageBox::Yes|QMessageBox::No,
- // QMessageBox::No);
- // if(ret==QMessageBox::Yes)
- // {
- //
- // this->close();
- // }
- }
main.cpp
- #include "mainwindow.h"
- #include "form.h"
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- Form f1;
- //连接W的信号和S的槽
- QObject::connect(&w,&MainWindow::jump,&f1,&Form::jump_slot);
- return a.exec();
- }
