• QT--day2


    form.h

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

    mainwindow.h

    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include "form.h"
    10. QT_BEGIN_NAMESPACE
    11. namespace Ui { class MainWindow; }
    12. QT_END_NAMESPACE
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. public:
    17. MainWindow(QWidget *parent = nullptr);
    18. ~MainWindow();
    19. private:
    20. Ui::MainWindow *ui;
    21. QLineEdit *zh;
    22. QLineEdit *mm;
    23. Form *f1;
    24. signals:
    25. void my_sig();
    26. void jump();
    27. public slots:
    28. void btn1_slot();
    29. void btn2_slot();
    30. };
    31. #endif // MAINWINDOW_H

    form.cpp

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

    mainwindow.cpp

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. MainWindow::MainWindow(QWidget *parent)
    4. : QMainWindow(parent)
    5. , ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. //设置固定大小
    9. this->setFixedSize(400,300);
    10. //设置窗口标题
    11. this->setWindowTitle("HUAQI");
    12. //设置窗口图标
    13. this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
    14. //构建两个按钮
    15. QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
    16. btn1->resize(100,40);
    17. btn1->move(100,250);
    18. QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
    19. btn2->resize(btn1->size());
    20. btn2->move(btn1->x()+150,btn1->y());
    21. connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_slot()));
    22. //构建两个行编辑器
    23. QLineEdit *edit1=new QLineEdit(this);
    24. edit1->resize(200,30);
    25. edit1->setEchoMode(QLineEdit::Password);
    26. edit1->setText("123456");
    27. edit1->move(125,btn1->y()-50);
    28. QLineEdit *edit2=new QLineEdit(this);
    29. edit2->resize(200,30);
    30. edit2->setText("admin");
    31. edit2->move(125,edit1->y()-50);
    32. //构建三个标签
    33. QLabel *lab1=new QLabel(this);
    34. lab1->resize(30,30);
    35. lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
    36. lab1->setScaledContents(true);
    37. lab1->move(edit1->x()-40,edit1->y());
    38. QLabel *lab2=new QLabel(this);
    39. lab2->resize(lab1->size());
    40. lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
    41. lab2->setScaledContents(true);
    42. lab2->move(edit2->x()-40,edit2->y());
    43. QLabel *lab3=new QLabel(this);
    44. lab3->resize(400,120);
    45. lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
    46. lab3->setScaledContents(true);
    47. connect(btn1,&QPushButton::clicked,this,&MainWindow::btn1_slot);
    48. zh=edit2;
    49. mm=edit1;
    50. }
    51. MainWindow::~MainWindow()
    52. {
    53. delete ui;
    54. }
    55. void MainWindow::btn1_slot()
    56. {
    57. if(mm->text()=="123456"&&zh->text()=="admin")
    58. {
    59. QMessageBox box(QMessageBox::NoIcon,
    60. "成功",
    61. "登陆成功",
    62. QMessageBox::Ok,
    63. this);
    64. int ret=box.exec();
    65. if(ret==QMessageBox::Ok)
    66. {
    67. emit jump();
    68. }
    69. //静态
    70. //QMessageBox box(this,
    71. // "成功",
    72. // "登陆成功",
    73. // QMessageBox::Ok,
    74. // QMessageBox::Ok);
    75. // int ret=box.exec();
    76. // if(ret==QMessageBox::Ok)
    77. // {
    78. //
    79. // emit jump();
    80. // }
    81. }else
    82. {
    83. QMessageBox box(QMessageBox::Warning,
    84. "错误",
    85. "账号或密码出错",
    86. QMessageBox::Yes|QMessageBox::No,
    87. this);
    88. box.setButtonText(QMessageBox::Yes,"继续");
    89. box.setButtonText(QMessageBox::No,"取消");
    90. int ret=box.exec();
    91. if(ret==QMessageBox::Yes)
    92. {
    93. }else
    94. {
    95. this->close();
    96. }
    97. //静态
    98. //int ret=QMessageBox::warning(this,
    99. // "错误",
    100. // "账号或密码出错",
    101. // QMessageBox::Yes|QMessageBox::No,
    102. // QMessageBox::No);
    103. // if(ret==QMessageBox::Yes)
    104. // {
    105. // }else
    106. // {
    107. // this->close();
    108. // }
    109. }
    110. }
    111. void MainWindow::btn2_slot()
    112. {
    113. QMessageBox box(QMessageBox::Question,
    114. "退出",
    115. "确定要退出吗?",
    116. QMessageBox::Yes|QMessageBox::No,
    117. this);
    118. int ret=box.exec();
    119. if(ret==QMessageBox::Yes)
    120. {
    121. this->close();
    122. }
    123. //静态
    124. //int ret=QMessageBox::warning(this,
    125. // "退出",
    126. // "确定要退出吗?",
    127. // QMessageBox::Yes|QMessageBox::No,
    128. // QMessageBox::No);
    129. // if(ret==QMessageBox::Yes)
    130. // {
    131. //
    132. // this->close();
    133. // }
    134. }

    main.cpp

    1. #include "mainwindow.h"
    2. #include "form.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. Form f1;
    10. //连接W的信号和S的槽
    11. QObject::connect(&w,&MainWindow::jump,&f1,&Form::jump_slot);
    12. return a.exec();
    13. }

  • 相关阅读:
    jzoj1212 重建道路
    vivado产生报告阅读分析14-时序报告10
    1、互联网核心岗位缩写
    网络代理技术:保护隐私与增强网络安全
    20220729NOI模拟赛--考后总结
    135. 分发糖果
    最新千万级中文语音语料开源数据整理分享
    WeetCode2滑动窗口系列
    Spark项目实战-卡口流量统计
    10、SpringBoot_测试用例
  • 原文地址:https://blog.csdn.net/m0_70569664/article/details/132998093