Qt(day2)
- 互联网
- 2025-08-19 06:36:02

思维导图 小练习 完善登录框 点击登录按钮后,判断账号(admin)和密码(123456)是否一致,如果匹配失败,则弹出错误对话框,文本内容“账号密码不匹配,是否重新登录”,给定两个按钮ok和cancel,点击ok后,会清除密码框中的内容,继续进行登录;如果点击cancel按钮,则关闭界面。 如果账号和密码匹配,则弹出信息对话框,给出提示信息为“登录成功”,给出一个按钮ok,点击ok后,关闭整个登录界面,跳转到其他界面 点击取消按钮后,弹出问题对话框,询问是否确定要退出登录,给出两个按钮,yes|no,点击yes,则直接关闭整个登录界面,如果点击no则进行进行登录 要求:消息对话框,对象版和静态成员函数版至少各实现一个 mywnd.h #ifndef MYWND_H #define MYWND_H #include <QMainWindow> #include<QLabel> #include<QLineEdit> #include<QPushButton> #include<QDebug> #include<QMessageBox> #include"second.h" QT_BEGIN_NAMESPACE namespace Ui { class MyWnd; } QT_END_NAMESPACE class MyWnd : public QMainWindow { Q_OBJECT signals: void btn1_signal(); public slots: void btn1_slot(); void btn2_slot(); public: MyWnd(QWidget *parent = nullptr); ~MyWnd(); private: Ui::MyWnd *ui; QPushButton *btn1; QPushButton *btn2; QLineEdit *edit1; QLineEdit *edit2; QLabel *lab1; QLabel *lab2; QLabel *lab3; Second *s1; }; #endif // MYWND_H second.h #ifndef SECOND_H #define SECOND_H #include <QWidget> namespace Ui { class Second; } class Second : public QWidget { Q_OBJECT public slots: void jump_slot(); public: explicit Second(QWidget *parent = nullptr); ~Second(); private: Ui::Second *ui; }; #endif // SECOND_H mywnd.cpp #include "mywnd.h" #include "ui_mywnd.h" MyWnd::MyWnd(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MyWnd) { ui->setupUi(this); //将当前界面的信号,与s1界面的槽函数进行连接 s1=new Second; connect(this,&MyWnd::btn1_signal,s1,&Second::jump_slot); //对页面进行修改 this->setFixedSize(QSize(800,700)); //固定文件框的大小 this->setWindowTitle("华清远见"); //设置文件的标题 this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png")); //为标题加图片 //设置logo图片 lab1=new QLabel(this); //构造一个lab,指定父组件 lab1->resize(800,250); //设置图片尺寸 lab1->setPixmap(QPixmap(":/icon/logo.png")); //设置logo图片 lab1->setScaledContents(true); //设置图片自适应 //设置username图片 lab2=new QLabel(this); //构造一个lab,指定父组件 lab2->resize(50,50); //设置图片尺寸 lab2->move(230,300); //设置图片位置 lab2->setPixmap(QPixmap(":/icon/username.jpg")); //设置logo图片 lab2->setScaledContents(true); //设置图片自适应 //设置passwd图片 lab3=new QLabel(this); //构造一个lab,指定父组件 lab3->resize(50,50); //设置图片尺寸 lab3->move(230,380); //设置图片位置 lab3->setPixmap(QPixmap(":/icon/passwd.jpg")); //设置logo图片 lab3->setScaledContents(true); //设置图片自适应 //设置username输入文本框 edit1=new QLineEdit(this); //构造一个行编辑器,指定父组件 edit1->resize(300,50); //设置行编辑器大小 edit1->move(300,300); //设置行编辑器位置 edit1->setEchoMode(QLineEdit::Normal); //设置明文模式 edit1->setPlaceholderText("username"); //设置占位符 //设置passwd输入文本框 edit2=new QLineEdit(this); //构造一个行编辑器,指定父组件 edit2->resize(300,50); //设置行编辑器大小 edit2->move(300,380); //设置行编辑器位置 edit2->setEchoMode(QLineEdit::Password); //设置密文模式 edit2->setPlaceholderText("passwd"); //设置占位符 //设置登录按钮 btn1=new QPushButton("登录",this); //构造登录按键,指定父组件 btn1->resize(380,70); //设置按键大小 btn1->setIcon(QIcon(":/icon/login.png")); //设置按键图标 btn1->move(lab3->x(),lab3->y()+80); //设置按键位置 //设置取消按钮 btn2=new QPushButton("取消",this); //构造取消按键,指定父组件 btn2->resize(380,70); //设置按键大小 btn2->setIcon(QIcon(":/icon/cancel.png")); //设置按键图标 btn2->move(btn1->x(),btn1->y()+90); //设置按键位置 //登录按键的信号与槽连接 connect(btn1,&QPushButton::clicked,this,&MyWnd::btn1_slot); //取消按键的信号与槽连接 connect(btn2,&QPushButton::clicked,this,&MyWnd::btn2_slot); } MyWnd::~MyWnd() { delete ui; } void MyWnd::btn1_slot(){ QMessageBox box(this); //基于属性版本实现消息对话框 if(edit1->text()=="admin"&&edit2->text()=="123456"){ box.setIcon(QMessageBox::Information); box.setWindowTitle("登陆成功"); box.setText("登陆成功"); box.setStandardButtons(QMessageBox::Ok); box.setDefaultButton(QMessageBox::Ok); box.exec(); emit btn1_signal(); //发射跳转的信号 this->hide(); //隐藏第一个页面 } else{ box.setIcon(QMessageBox::Question); box.setWindowTitle("匹配错误"); box.setText("账号密码不匹配,是否重新登录?"); box.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel); box.setDefaultButton(QMessageBox::Ok); int ret=box.exec(); if(ret==QMessageBox::Ok){ edit1->clear(); //清空用户框中的内容 edit2->clear(); //清空密码框中的内容 } else{ this->close(); //关闭页面 } } } void MyWnd::btn2_slot(){ int ret=QMessageBox::warning(this, //基于静态成员函数版本的消息对话框 "是否退出", "是否确定要退出登录?", QMessageBox::Yes|QMessageBox::No, QMessageBox::No); if(ret==QMessageBox::Yes){ this->close(); } } 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(); } main.cpp #include "mywnd.h" #include "second.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWnd w; w.show(); Second s; //在主调函数中将第一个界面的信号与第二个界面的槽函数连接 //QObject::connect(&w,&MyWnd::btn1_signal,&s,&Second::jump_slot); return a.exec(); }