当前位置:   article > 正文

8.16作业Qt

8.16作业Qt

登录页面进行进一步优化,发挥信号和槽的作用

1.  .ui文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Widget</class>
  4. <widget class="QWidget" name="Widget">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>800</width>
  10. <height>600</height>
  11. </rect>
  12. </property>
  13. <property name="acceptDrops">
  14. <bool>false</bool>
  15. </property>
  16. <property name="windowTitle">
  17. <string>QQ</string>
  18. </property>
  19. <property name="windowIcon">
  20. <iconset>
  21. <normaloff>R-C.jpg</normaloff>R-C.jpg</iconset>
  22. </property>
  23. <property name="styleSheet">
  24. <string notr="true">background-color: rgb(255, 255, 255);</string>
  25. </property>
  26. <widget class="QLabel" name="label">
  27. <property name="geometry">
  28. <rect>
  29. <x>0</x>
  30. <y>0</y>
  31. <width>800</width>
  32. <height>250</height>
  33. </rect>
  34. </property>
  35. <property name="styleSheet">
  36. <string notr="true">background-color: rgb(19, 255, 78);</string>
  37. </property>
  38. <property name="text">
  39. <string/>
  40. </property>
  41. <property name="scaledContents">
  42. <bool>false</bool>
  43. </property>
  44. <property name="openExternalLinks">
  45. <bool>false</bool>
  46. </property>
  47. </widget>
  48. <widget class="QLabel" name="label_2">
  49. <property name="geometry">
  50. <rect>
  51. <x>220</x>
  52. <y>320</y>
  53. <width>40</width>
  54. <height>40</height>
  55. </rect>
  56. </property>
  57. <property name="text">
  58. <string/>
  59. </property>
  60. <property name="pixmap">
  61. <pixmap>R-C.jpg</pixmap>
  62. </property>
  63. <property name="scaledContents">
  64. <bool>true</bool>
  65. </property>
  66. </widget>
  67. <widget class="QLabel" name="label_3">
  68. <property name="enabled">
  69. <bool>true</bool>
  70. </property>
  71. <property name="geometry">
  72. <rect>
  73. <x>220</x>
  74. <y>380</y>
  75. <width>40</width>
  76. <height>40</height>
  77. </rect>
  78. </property>
  79. <property name="text">
  80. <string>密码</string>
  81. </property>
  82. <property name="scaledContents">
  83. <bool>false</bool>
  84. </property>
  85. <property name="wordWrap">
  86. <bool>false</bool>
  87. </property>
  88. </widget>
  89. <widget class="QLineEdit" name="lineEdit">
  90. <property name="geometry">
  91. <rect>
  92. <x>290</x>
  93. <y>320</y>
  94. <width>250</width>
  95. <height>40</height>
  96. </rect>
  97. </property>
  98. <property name="text">
  99. <string notr="true"/>
  100. </property>
  101. </widget>
  102. <widget class="QLineEdit" name="lineEdit_2">
  103. <property name="geometry">
  104. <rect>
  105. <x>290</x>
  106. <y>380</y>
  107. <width>250</width>
  108. <height>40</height>
  109. </rect>
  110. </property>
  111. <property name="autoFillBackground">
  112. <bool>false</bool>
  113. </property>
  114. <property name="inputMask">
  115. <string/>
  116. </property>
  117. <property name="text">
  118. <string notr="true"/>
  119. </property>
  120. <property name="frame">
  121. <bool>true</bool>
  122. </property>
  123. <property name="echoMode">
  124. <enum>QLineEdit::Normal</enum>
  125. </property>
  126. <property name="dragEnabled">
  127. <bool>false</bool>
  128. </property>
  129. <property name="readOnly">
  130. <bool>false</bool>
  131. </property>
  132. <property name="placeholderText">
  133. <string/>
  134. </property>
  135. <property name="clearButtonEnabled">
  136. <bool>false</bool>
  137. </property>
  138. </widget>
  139. <widget class="QLabel" name="label_4">
  140. <property name="geometry">
  141. <rect>
  142. <x>330</x>
  143. <y>550</y>
  144. <width>120</width>
  145. <height>16</height>
  146. </rect>
  147. </property>
  148. <property name="text">
  149. <string/>
  150. </property>
  151. </widget>
  152. <widget class="QLabel" name="label_5">
  153. <property name="enabled">
  154. <bool>false</bool>
  155. </property>
  156. <property name="geometry">
  157. <rect>
  158. <x>0</x>
  159. <y>250</y>
  160. <width>800</width>
  161. <height>350</height>
  162. </rect>
  163. </property>
  164. <property name="text">
  165. <string/>
  166. </property>
  167. <property name="scaledContents">
  168. <bool>true</bool>
  169. </property>
  170. </widget>
  171. <zorder>label_5</zorder>
  172. <zorder>label</zorder>
  173. <zorder>label_2</zorder>
  174. <zorder>label_3</zorder>
  175. <zorder>lineEdit</zorder>
  176. <zorder>lineEdit_2</zorder>
  177. <zorder>label_4</zorder>
  178. </widget>
  179. <resources/>
  180. <connections/>
  181. </ui>

2.头函数

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QPushButton>
  5. #include <QLabel>
  6. #include <QLineEdit>
  7. #include <QMovie>
  8. #include <cstring>
  9. #include <QString>
  10. #include <QDebug>
  11. #include <ctime>
  12. #include <thread>
  13. #include <chrono>
  14. #include <QThread>
  15. QT_BEGIN_NAMESPACE
  16. namespace Ui { class Widget; }
  17. QT_END_NAMESPACE
  18. void abc();
  19. class Widget : public QWidget
  20. {
  21. Q_OBJECT
  22. friend void abc();
  23. public:
  24. Widget(QWidget *parent = nullptr);
  25. ~Widget();
  26. signals:
  27. void on_sig();
  28. void on_si();
  29. public slots:
  30. void my_slot();
  31. void my_slot2();
  32. void my_slot3();
  33. private:
  34. Ui::Widget *ui;
  35. QPushButton *p1;
  36. };
  37. class WorkerThread : public QThread
  38. {
  39. Q_OBJECT
  40. public:
  41. void run() override {
  42. std::this_thread::sleep_for(std::chrono::seconds(10));
  43. emit resultReady();
  44. }
  45. signals:
  46. void resultReady();
  47. };
  48. #endif // WIDGET_H

3.功能函数

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::Widget)
  6. ,p1(new QPushButton("登录",this))
  7. {
  8. connect(p1,SIGNAL(clicked()),this,SLOT(my_slot()));
  9. ui->setupUi(this);
  10. this->setWindowIcon(QIcon(":/R-C.jpg"));
  11. QMovie *mv2 = new QMovie(":/OIP2.gif");
  12. ui->label_5->setMovie(mv2);
  13. mv2->start();
  14. ui->label_5->setScaledContents(true);
  15. QMovie *mv1 = new QMovie(":/R-C.gif");
  16. ui->label->setMovie(mv1);
  17. mv1->start();
  18. ui->label->setScaledContents(true);
  19. ui->label_2->setPixmap(QPixmap(":/R-C.jpg"));
  20. ui->label_2->setScaledContents(true);
  21. ui->label_3->setPixmap(QPixmap(":/OIP-C.jpg"));
  22. ui->label_3->setScaledContents(true);
  23. ui->lineEdit->setPlaceholderText("请输入QQ号\\手机号");
  24. ui->lineEdit_2->setPlaceholderText("密码");
  25. ui->lineEdit_2->setEchoMode(QLineEdit::Password);
  26. p1->raise();
  27. p1->resize(300,40);
  28. p1->move(250,430);
  29. p1->setStyleSheet("background-color:rgb(31,200,253);color:white;\
  30. border-radius:5px");
  31. }
  32. Widget::~Widget()
  33. {
  34. delete ui;
  35. }
  36. void Widget::my_slot()
  37. {
  38. char s1[10]="123456";
  39. char s2[10]="123456";
  40. if(ui->lineEdit->text()==s1&&ui->lineEdit_2->text()==s2)
  41. {
  42. qDebug() << "登陆成功";
  43. p1->setText("登录成功");
  44. WorkerThread *worker = new WorkerThread();//分离式线程时间延迟
  45. worker->start();
  46. connect(worker, &WorkerThread::resultReady,this,&Widget::my_slot2);
  47. }
  48. else
  49. {
  50. p1->setText("登陆失败、请重新登录");
  51. p1->setStyleSheet("background-color:rgb(51,200,253)");
  52. ui->lineEdit->clear();
  53. ui->lineEdit_2->clear();
  54. WorkerThread *worker = new WorkerThread();
  55. worker->start();
  56. connect(worker, &WorkerThread::resultReady,this,&Widget::my_slot3);
  57. }
  58. }
  59. void Widget::my_slot2()
  60. {
  61. qDebug() << "关闭";
  62. close();
  63. }
  64. void Widget::my_slot3()
  65. {
  66. p1->setText("登录");
  67. p1->setStyleSheet("background-color:rgb(31,200,253);color:white;\
  68. border-radius:5px");
  69. }

登录失败效果

 登录成功效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1005475
推荐阅读
相关标签
  

闽ICP备14008679号