• 华清 Qt day5 9月21


    1. QT += core gui sql network
    2. /*****************************************************************/
    3. #ifndef WIDGET_H
    4. #define WIDGET_H
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include "second.h"
    14. #include
    15. #include
    16. #include
    17. QT_BEGIN_NAMESPACE
    18. namespace Ui { class Widget; }
    19. QT_END_NAMESPACE
    20. class Widget : public QWidget
    21. {
    22. Q_OBJECT
    23. public:
    24. Widget(QWidget *parent = nullptr);
    25. ~Widget();
    26. private slots:
    27. void on_btn1_clicked();
    28. void on_btn2_clicked();
    29. void on_le1_cursorPositionChanged(int arg1, int arg2);
    30. void on_le2_cursorPositionChanged(int arg1, int arg2);
    31. void on_btn3_clicked();
    32. signals:
    33. void jump(); //定义一个跳转到其他界面的信号
    34. private:
    35. Ui::Widget *ui;
    36. Second *s1;
    37. QSqlDatabase db; //定义一个数据库的类对象
    38. };
    39. #endif // WIDGET_H
    40. /*****************************************************************/
    41. #ifndef SECOND_H
    42. #define SECOND_H
    43. #include
    44. #include
    45. #include
    46. namespace Ui {
    47. class Second;
    48. }
    49. class Second : public QDialog
    50. {
    51. Q_OBJECT
    52. public:
    53. explicit Second(QWidget *parent = nullptr);
    54. ~Second();
    55. public slots:
    56. void jump_slot(); //自定义了一个槽函数
    57. private slots:
    58. void on_pushButton_2_clicked();
    59. void connected_slot();
    60. void readyRead_slot();
    61. void on_send_clicked();
    62. void on_duankai_clicked();
    63. void disconnected_slot();
    64. private:
    65. Ui::Second *ui;
    66. //定义一个客户端指针
    67. QTcpSocket *socket;
    68. //定义字符串用于接收用户名
    69. QString username;
    70. };
    71. #endif // SECOND_H
    72. /*****************************************************************/
    73. #include "widget.h"
    74. #include "ui_widget.h"
    75. Widget::Widget(QWidget *parent)
    76. : QWidget(parent)
    77. , ui(new Ui::Widget)
    78. {
    79. ui->setupUi(this);
    80. s1 = new Second;
    81. connect(this,&Widget::jump,s1,&Second::jump_slot); //将jump信号绑定到Second的槽函数
    82. //添加一个数据库
    83. if(!db.contains("id_passwd.db"))
    84. {
    85. db = QSqlDatabase::addDatabase("QSQLITE");
    86. db.setDatabaseName("id_passwd.db");
    87. }
    88. //打开数据库
    89. if(!db.open())
    90. {
    91. QMessageBox::information(this,"失败","数据库打开失败");
    92. return;
    93. }
    94. //创建表格,准备sql语句
    95. QString sql = "create table if not exists id_passwd("
    96. "id varchar(10) primary key,"
    97. "passwd varchar(20))";
    98. //准备语句执行者
    99. QSqlQuery querry;
    100. if(!querry.exec(sql))
    101. {
    102. QMessageBox::information(this,"失败","创建表失败");
    103. }
    104. }
    105. Widget::~Widget()
    106. {
    107. delete ui;
    108. }
    109. //登录按钮
    110. void Widget::on_btn1_clicked()
    111. {
    112. QString username = ui->le1->text();
    113. QString password = ui->le2->text();
    114. QString sql = QString("select * from id_passwd where id = '%1' "
    115. "and passwd = '%2'").arg(username).arg(password);
    116. QSqlQuery q;
    117. //登录成功
    118. if (q.exec(sql)&&q.next())
    119. {
    120. QMessageBox::information(this,"登录成功","登录成功");
    121. //跳转到其他界面的代码
    122. emit jump(); //使用关键字emit发射自定义的信号
    123. this->hide();
    124. }
    125. else //登录失败
    126. {
    127. //静态版
    128. int ret = QMessageBox::critical(this,"登录失败",
    129. "账号密码不匹配,是否重新登录",
    130. QMessageBox::Ok|QMessageBox::Cancel);
    131. if(ret==QMessageBox::Ok)
    132. {
    133. //清楚密码框内容
    134. ui->le2->clear();
    135. }
    136. else if(ret==QMessageBox::Cancel)
    137. {
    138. //关闭界面
    139. close();
    140. }
    141. }
    142. }
    143. //取消按钮
    144. void Widget::on_btn2_clicked()
    145. {
    146. //对象版
    147. QMessageBox box(QMessageBox::Question,"退出登录","是否确定退出登录?",
    148. QMessageBox::Yes|QMessageBox::No,
    149. this);
    150. int ret = box.exec();
    151. if(ret == QMessageBox::Yes)
    152. {
    153. //关闭界面
    154. close();
    155. }
    156. }
    157. //输入账号
    158. void Widget::on_le1_cursorPositionChanged(int arg1, int arg2)
    159. {
    160. ui->le1->setPlaceholderText("账号");
    161. }
    162. //输入密码
    163. void Widget::on_le2_cursorPositionChanged(int arg1, int arg2)
    164. {
    165. ui->le2->setPlaceholderText("密码");
    166. ui->le2->setEchoMode(QLineEdit::Password);
    167. ui->le2->setMaxLength(12);
    168. }
    169. //注册按钮对应的槽函数
    170. void Widget::on_btn3_clicked()
    171. {
    172. QString id = ui->le1->text();
    173. QString passwd = ui->le2->text();
    174. if(id.isEmpty()||passwd.isEmpty())
    175. {
    176. QMessageBox::information(this,"提示","填写完整账号或者密码");
    177. return;
    178. }
    179. //准备sql语句
    180. QString sql = QString("insert into id_passwd(id,passwd)"
    181. "values('%1','%2')").arg(id).arg(passwd);
    182. //准备语句的执行者
    183. QSqlQuery querry;
    184. if(!querry.exec(sql))
    185. {
    186. QMessageBox::information(this,"失败","注册失败");
    187. return;
    188. }
    189. else
    190. {
    191. QMessageBox::information(this,"成功","注册成功");
    192. }
    193. }
    194. /*****************************************************************/
    195. #include "second.h"
    196. #include "ui_second.h"
    197. Second::Second(QWidget *parent) :
    198. QDialog(parent),
    199. ui(new Ui::Second)
    200. {
    201. ui->setupUi(this);
    202. //给客户端指针实例化对象
    203. socket = new QTcpSocket(this);
    204. connect(socket,&QTcpSocket::connected,this,&Second::connected_slot);
    205. connect(socket,&QTcpSocket::readyRead,this,&Second::readyRead_slot);
    206. connect(socket,&QTcpSocket::disconnected,this,&Second::disconnected_slot);
    207. }
    208. Second::~Second()
    209. {
    210. delete ui;
    211. }
    212. void Second::jump_slot()
    213. {
    214. this->show(); //当接收到信号,展示自己的界面
    215. }
    216. //连接按钮对应的槽函数
    217. void Second::on_pushButton_2_clicked()
    218. {
    219. //获取UI界面上的相关信息
    220. username = ui->le_usr->text();
    221. QString ip = ui->le_ip->text();
    222. quint16 port = ui->le_port->text().toUInt();
    223. //将客户端连接到指定的服务器
    224. socket->connectToHost(ip,port);
    225. //连接成功的话客户端会自动发送一个connected的信号
    226. }
    227. void Second::connected_slot()
    228. {
    229. QMessageBox::information(this,"成功","连接服务器成功");
    230. QString msg = username + ":进入聊天室";
    231. socket->write(msg.toLocal8Bit()); //将数据发给服务器
    232. }
    233. void Second::readyRead_slot()
    234. {
    235. //读取服务器发来的信息
    236. QByteArray msg = socket->readAll();
    237. //将数据展示到界面
    238. ui->listWidget->addItem(QString::fromLocal8Bit(msg));
    239. }
    240. //发送按钮对应的槽函数
    241. void Second::on_send_clicked()
    242. {
    243. //读取UI界面中输入的内容
    244. QString msg = username + ": " + ui->text->text();
    245. //发送给服务器
    246. socket->write(msg.toLocal8Bit());
    247. ui->text->clear();
    248. }
    249. //断开按钮对应的槽函数
    250. void Second::on_duankai_clicked()
    251. {
    252. QString msg = username + " 离开服务器";
    253. socket->write(msg.toLocal8Bit());
    254. //断开连接
    255. socket->disconnectFromHost();
    256. //此时,客户端会自动发射一个disconnected信号
    257. }
    258. void Second::disconnected_slot()
    259. {
    260. QMessageBox::information(this,"提示","退出成功");
    261. }

     

     

     

     QT - day5 - GitMind

     QT - day4 - GitMind

    面试题 - GitMind 

  • 相关阅读:
    用 Golang 从0到1实现一个高性能的 Worker Pool(一) - 每天5分钟玩转 GPT 编程系列(3)
    代码设计:C++ 一个CSV功能类(源码)
    【STM32】存储器和位带映射(bit band mapping)
    【408篇】C语言笔记-第九章(数据结构概述)
    【20221114】【每日一题】子集
    js基础笔记学习257location
    颜色杂项笔记
    C++程序设计——类和对象(上)
    vsFTP简单安装测试
    C 语言 sizeof运算符
  • 原文地址:https://blog.csdn.net/Huxiao1220/article/details/133148445