• QT-day2


    完善登录框

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

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

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

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

    加载资源文件

    mywnd.h

    1. #ifndef MYWND_H
    2. #define MYWND_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. class MyWnd : public QWidget
    10. {
    11. Q_OBJECT
    12. signals:
    13. void my_signal();
    14. void jumpsignal();
    15. public:
    16. MyWnd(QWidget *parent = nullptr);
    17. ~MyWnd();
    18. private slots:
    19. void loginbton1_clicked();
    20. void cancelbton2_clicked();
    21. private:
    22. QPushButton *bton1;
    23. QPushButton *bton2;
    24. QLineEdit *edit1;
    25. QLineEdit *edit2;
    26. QLabel *lab1;
    27. QLabel *lab2;
    28. QLabel *lab3;
    29. };
    30. #endif // MYWND_H

    second.h

    1. #ifndef SECOND_H
    2. #define SECOND_H
    3. #include
    4. namespace Ui {
    5. class Second;
    6. }
    7. class Second : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. void jump_slot();
    12. public:
    13. explicit Second(QWidget *parent = nullptr);
    14. ~Second();
    15. private:
    16. Ui::Second *ui;
    17. };
    18. #endif // SECOND_H

    main.cpp

    1. #include "mywnd.h"
    2. #include "second.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MyWnd w;
    8. w.show();
    9. Second s;
    10. QObject::connect(&w,&MyWnd::jumpsignal,&s,&Second::jump_slot);
    11. return a.exec();
    12. }

    mywnd.cpp

    1. #include "mywnd.h"
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. MyWnd::MyWnd(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10. this->setFixedSize(400,350);
    11. this->setWindowTitle("Widget");
    12. this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));
    13. QLabel *lab1 = new QLabel(this);
    14. lab1->resize(400,150);
    15. lab1->setPixmap(QPixmap(":/icon/logo.png"));
    16. lab1->setScaledContents(true);
    17. QLabel *lab2 = new QLabel(this);
    18. lab2->resize(60,55);
    19. lab2->move(75,160);
    20. lab2->setPixmap(QPixmap(":/icon/userName.jpg"));
    21. lab2->setScaledContents(true);
    22. QLabel *lab3 = new QLabel(this);
    23. lab3->resize(60,55);
    24. lab3->move(lab2->x(),lab2->y()+65);
    25. lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
    26. lab3->setScaledContents(true);
    27. QLineEdit *edit1 = new QLineEdit("admin",this);
    28. edit1->resize(200,55);
    29. edit1->move(lab2->x()+65,lab2->y());
    30. QLineEdit *edit2 = new QLineEdit("123456",this);
    31. edit2->resize(200,55);
    32. edit2->move(lab3->x()+65,lab3->y());
    33. edit2->setEchoMode(QLineEdit::Password);
    34. QPushButton *bton1 = new QPushButton(QIcon(":/icon/login.png"),"登录",this);
    35. bton1->resize(90,55);
    36. bton1->move(edit2->x()+20,edit2->y()+65);
    37. QPushButton *bton2 = new QPushButton(QIcon(":/icon/cancel.png"),"取消",this);
    38. bton2->resize(90,55);
    39. bton2->move(bton1->x()+105,bton1->y());
    40. connect(bton1,&QPushButton::clicked,this,&MyWnd::loginbton1_clicked);
    41. connect(bton2,&QPushButton::clicked,this,&MyWnd::cancelbton2_clicked);
    42. }
    43. void MyWnd::loginbton1_clicked()
    44. {
    45. if(edit1->text()=="admin"&&edit2->text()=="123456"){
    46. QMessageBox sucbox(QMessageBox::Information,
    47. "登录",
    48. "登录成功",
    49. QMessageBox::Ok,
    50. this);
    51. int ret = sucbox.exec();
    52. if(ret == QMessageBox::Ok){
    53. this->close();
    54. emit jumpsignal();
    55. }
    56. }else{
    57. QMessageBox errbox(QMessageBox::NoIcon,
    58. "登录",
    59. "账号密码不匹配,是否重新登录",
    60. QMessageBox::Ok|QMessageBox::Cancel,
    61. this);
    62. int ret = errbox.exec();
    63. if(ret == QMessageBox::Ok){
    64. edit2->clear();
    65. }else if(ret == QMessageBox::Cancel){
    66. this->close();
    67. }
    68. }
    69. }
    70. void MyWnd::cancelbton2_clicked()
    71. {
    72. int ret = QMessageBox::question(this,
    73. "取消",
    74. "是否确定要退出登录",
    75. QMessageBox::Yes|QMessageBox::No,
    76. QMessageBox::Yes);
    77. if(ret == QMessageBox::Yes){
    78. this->close();
    79. }
    80. }
    81. MyWnd::~MyWnd()
    82. {
    83. }

    second.cpp

    1. #include "second.h"
    2. #include "ui_second.h"
    3. Second::Second(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Second)
    6. {
    7. ui->setupUi(this);
    8. }
    9. Second::~Second()
    10. {
    11. delete ui;
    12. }
    13. void Second::jump_slot()
    14. {
    15. this->show();
    16. }

    测试结果

    思维导图

  • 相关阅读:
    DDIM代码详细解读(3):核心采样代码、超分辨率重建
    dart 学习 之命名参数
    贪心算法之活动安排
    服务器虚拟化有什么好处
    软件系统建模&架构风格-架构论文(三十八)
    Spring Security入门
    Spring ,Spring MVC,Spring Boot
    使用mybatis或mybatisplus存储数据库加密,解密
    CISP-PTE实操练习题讲解一(新版)
    共享充电语音提醒功能如何实现?
  • 原文地址:https://blog.csdn.net/m0_64549633/article/details/132997370