• QTday5


    注册

    1. widget.h
    2. #ifndef WIDGET_H
    3. #define WIDGET_H
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include //消息对话框类
    11. #include "second.h"
    12. #include //数据库管理类
    13. #include //执行sql语句类
    14. #include //数据库记录的类
    15. #include "zhuce.h" //注册头文件
    16. QT_BEGIN_NAMESPACE
    17. namespace Ui { class widget; }
    18. QT_END_NAMESPACE
    19. class widget : public QWidget
    20. {
    21. Q_OBJECT
    22. signals://该权限下定义属于自己的信号
    23. void my_signal(QString msg);//自定义一个有参无返回值的信号函数
    24. private:
    25. void on_btn1_clicked();//自定义的槽函数声明
    26. void on_btn2_clicked();//自定义的槽函数声明
    27. void on_btn3_clicked();
    28. public:
    29. widget(QWidget *parent = nullptr);
    30. ~widget();
    31. signals:
    32. void jump();//自定义跳转信号函数
    33. void jump1();
    34. public:
    35. //void jump_slot2();
    36. private:
    37. Ui::widget *ui;
    38. //自定义一个btn1
    39. QPushButton *btn1;
    40. QPushButton *btn2;
    41. QPushButton *btn3;
    42. QLineEdit *edit2;
    43. QLineEdit *edit1;
    44. Second *s1;
    45. QSqlDatabase db; //定义一个数据库的类对象
    46. zhuce *s2;
    47. };
    48. #endif // WIDGET_H
    49. zhuce.h
    50. #ifndef ZHUCE_H
    51. #define ZHUCE_H
    52. #include
    53. #include
    54. #include
    55. #include
    56. #include
    57. #include
    58. #include
    59. #include //执行sql语句类
    60. #include //数据库记录的类
    61. //#include "widget.h"
    62. namespace Ui {
    63. class zhuce;
    64. }
    65. class zhuce : public QWidget
    66. {
    67. Q_OBJECT
    68. private:
    69. void on_btn2_clicked();//自定义的槽函数声明
    70. void on_btn3_clicked();
    71. public:
    72. void jump_slot1();
    73. signals:
    74. //void jump2();
    75. public:
    76. explicit zhuce(QWidget *parent = nullptr);
    77. ~zhuce();
    78. private:
    79. Ui::zhuce *ui;
    80. QLineEdit *edit2;//密码
    81. QLineEdit *edit1;//用户名
    82. QLineEdit *edit3;//确认密码
    83. QPushButton *btn2;
    84. QPushButton *btn3;
    85. QSqlDatabase db; //定义一个数据库的类对象
    86. //widget *s3;
    87. };
    88. #endif // ZHUCE_H
    89. widget.cpp
    90. #include "widget.h"
    91. widget::widget(QWidget *parent)
    92. : QWidget(parent)
    93. {
    94. //登录成功的界面
    95. s1=new Second;
    96. connect(this,&widget::jump,s1,&Second::jump_slot);
    97. //注册的界面
    98. s2=new zhuce;
    99. connect(this,&widget::jump1,s2,&zhuce::jump_slot1);
    100. //添加数据库
    101. if(!db.contains("mysql.db"))
    102. {
    103. //添加数据库
    104. db=QSqlDatabase::addDatabase("QSQLITE");
    105. //设置数据库名字
    106. db.setDatabaseName("mysql.db");
    107. }
    108. //打开数据库
    109. if(!db.open())
    110. {
    111. QMessageBox::information(this,"失败","数据库打开失败");
    112. return;
    113. }
    114. //准备sql语句对表进行创建
    115. QString sql="create table if not exists stu_info("
    116. "name varchar(10) primary key,"//用户名
    117. "password varchar(20))";//密码
    118. //语句执行者
    119. QSqlQuery querry;
    120. if(!querry.exec(sql))
    121. {
    122. QMessageBox::information(this,"失败","数据库创建失败");
    123. return;
    124. }
    125. this->setFixedSize(550,400);//设置固定尺寸
    126. this->setWindowTitle("Widget");//设置窗口标题
    127. this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标
    128. //实例化一个标签
    129. QLabel *lab1 = new QLabel;
    130. lab1->setParent(this);
    131. lab1->resize(550,215);//重新设置尺寸
    132. //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    133. lab1->setPixmap(QPixmap("D:\\icon\\logo.png"));
    134. lab1->setScaledContents(true);//设置内容自适应
    135. QLabel *lab3 = new QLabel;
    136. lab3->setParent(this);//指定父组件
    137. lab3->resize(40,40);//重新设置尺寸
    138. lab3->move(100,220);
    139. //lab3->setAlignment(Qt::AlignCenter);
    140. lab3->setPixmap(QPixmap("D:\\icon\\userName.jpg"));
    141. lab3->setScaledContents(true);//设置内容自适应
    142. QLabel *lab2 = new QLabel;
    143. lab2->setParent(this);
    144. lab2->resize(40,40);//重新设置尺寸
    145. lab2->move(100,270);
    146. lab2->setPixmap(QPixmap("D:\\icon\\passwd.jpg"));
    147. lab2->setScaledContents(true);//设置内容自适应
    148. // 实例化一个行编辑器
    149. edit1 = new QLineEdit(this);
    150. edit1->resize(250,40);
    151. edit1->move(lab3->x()+50,lab3->y());
    152. // 实例化一个行编辑器
    153. edit2 = new QLineEdit(this);
    154. edit2->resize(250,40);
    155. edit2->move(lab2->x()+50,lab2->y());
    156. edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    157. //实例化一个按钮并给定图标,文本内容,父组件
    158. btn1 = new QPushButton(QIcon("D:\\icon\\login.png"), "登录", this);
    159. btn1->resize(100,50);//设置按钮大小
    160. btn1->move(225,325);//设置按钮移动位置
    161. connect(this->btn1,&QPushButton::clicked,this,&widget::on_btn1_clicked);
    162. btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    163. btn3->resize(btn1->size());//设置按钮大小
    164. btn3->move(btn1->x()-150,btn1->y());//设置按钮移动位置
    165. connect(this->btn3,&QPushButton::clicked,this,&widget::on_btn3_clicked);
    166. btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    167. btn2->resize(btn1->size());
    168. btn2->move(btn1->x()+150,btn1->y());
    169. connect(this->btn2,&QPushButton::clicked,this,&widget::on_btn2_clicked);
    170. }
    171. //登录按钮
    172. void widget::on_btn1_clicked()
    173. {
    174. //获取界面姓名
    175. QString name = this->edit1->text();
    176. //准备sql语句
    177. QString sql = QString("select * from stu_info where name='%1'").arg(name);
    178. //准备执行者
    179. QSqlQuery querry;
    180. if(!querry.exec(sql))
    181. {
    182. QMessageBox::information(this, "提示", "登录失败");
    183. return;
    184. }
    185. else
    186. {
    187. //1、调用构造函数实例化对象
    188. QMessageBox box(QMessageBox::Information,//图标
    189. "信息对话框",//对话框标题
    190. "登录成功",//对话框文本内容
    191. QMessageBox::Ok,//提供的按钮
    192. this);//父组件
    193. box.setDefaultButton(QMessageBox::Ok);//将OK设置为默认按钮
    194. //2、调用exec函数运行对话框
    195. int ret=box.exec();
    196. //3、对结果进行判断
    197. if(ret==QMessageBox::Ok)
    198. {
    199. emit jump();
    200. this->hide();
    201. }
    202. }
    203. }
    204. //取消按钮
    205. void widget::on_btn2_clicked()
    206. {
    207. //1、调用构造函数实例化对象
    208. QMessageBox box(QMessageBox::Question,//图标
    209. "问题对话框",//对话框标题
    210. "是否确定取消登录?",//对话框文本内容
    211. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    212. this);//父组件
    213. box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    214. //2、调用exec函数运行对话框
    215. int ret=box.exec();
    216. //3、对结果进行判断
    217. if(ret==QMessageBox::Yes)
    218. {
    219. close();
    220. }
    221. else if(ret==QMessageBox::No)
    222. {
    223. }
    224. }
    225. void widget::on_btn3_clicked()
    226. {
    227. emit jump1();
    228. //QWidget::hide();
    229. }
    230. widget::~widget()
    231. {
    232. }
    233. /*void widget::jump_slot2()
    234. {
    235. this->show();
    236. }*/
    237. zhuce.cpp
    238. #include "zhuce.h"
    239. #include "ui_zhuce.h"
    240. zhuce::zhuce(QWidget *parent) :
    241. QWidget(parent),
    242. ui(new Ui::zhuce)
    243. {
    244. //注册的界面
    245. //s3=new widget;
    246. //connect(this,&zhuce::jump2,s3,&widget::jump_slot2);
    247. this->setFixedSize(550,500);//设置固定尺寸
    248. this->setWindowTitle("注册");//设置窗口标题
    249. this->setWindowIcon(QIcon("D:\\icon\\wodepeizhenshi.png"));//设置窗口图标
    250. //实例化一个标签
    251. QLabel *lab = new QLabel;
    252. lab->setParent(this);
    253. lab->resize(550,215);//重新设置尺寸
    254. //lab1->setAlignment(Qt::AlignCenter);//文本对齐
    255. lab->setPixmap(QPixmap("D:\\icon\\logo.png"));
    256. lab->setScaledContents(true);//设置内容自适应
    257. ui->setupUi(this);
    258. QLabel *lab1 = new QLabel;
    259. lab1->setParent(this);//指定父组件
    260. lab1->resize(150,40);//重新设置尺寸
    261. lab1->move(100,220);
    262. //lab3->setAlignment(Qt::AlignCenter);
    263. lab1->setText("用户名:");
    264. lab1->setScaledContents(true);//设置内容自适应
    265. QLabel *lab2 = new QLabel;
    266. lab2->setParent(this);
    267. lab2->resize(150,40);//重新设置尺寸
    268. lab2->move(100,270);
    269. lab2->setText("密码:");
    270. lab2->setScaledContents(true);//设置内容自适应
    271. QLabel *lab3 = new QLabel;
    272. lab3->setParent(this);
    273. lab3->resize(150,40);//重新设置尺寸
    274. lab3->move(100,320);
    275. lab3->setText("确认密码:");
    276. lab3->setScaledContents(true);//设置内容自适应
    277. // 实例化一个行编辑器
    278. edit1 = new QLineEdit(this);
    279. edit1->resize(250,40);
    280. edit1->move(lab1->x()+80,lab1->y());
    281. // 实例化一个行编辑器
    282. edit2 = new QLineEdit(this);
    283. edit2->resize(250,40);
    284. edit2->move(lab2->x()+80,lab2->y());
    285. edit2->setEchoMode(QLineEdit::Password);//设置回显模式
    286. edit3 = new QLineEdit(this);
    287. edit3->resize(250,40);
    288. edit3->move(lab3->x()+80,lab3->y());
    289. edit3->setEchoMode(QLineEdit::Password);//设置回显模式
    290. btn3 = new QPushButton(QIcon("D:\\icon\\R-C.jpg"), "注册", this);
    291. btn3->resize(100,50);//设置按钮大小
    292. btn3->move(125,400);//设置按钮移动位置
    293. connect(this->btn3,&QPushButton::clicked,this,&zhuce::on_btn3_clicked);
    294. btn2 = new QPushButton(QIcon("D:\\icon\\cancel.png"),"取消", this);
    295. btn2->resize(100,50);
    296. btn2->move(325,400);
    297. connect(this->btn2,&QPushButton::clicked,this,&zhuce::on_btn2_clicked);
    298. //添加数据库
    299. if(!db.contains("mysql.db"))
    300. {
    301. //添加数据库
    302. db=QSqlDatabase::addDatabase("QSQLITE");
    303. //设置数据库名字
    304. db.setDatabaseName("mysql.db");
    305. }
    306. //打开数据库
    307. if(!db.open())
    308. {
    309. QMessageBox::information(this,"失败","数据库打开失败");
    310. return;
    311. }
    312. }
    313. zhuce::~zhuce()
    314. {
    315. delete ui;
    316. }
    317. //取消
    318. void zhuce::on_btn2_clicked()
    319. {
    320. //1、调用构造函数实例化对象
    321. QMessageBox box(QMessageBox::Question,//图标
    322. "问题对话框",//对话框标题
    323. "是否确定取消注册?",//对话框文本内容
    324. QMessageBox::Yes|QMessageBox::No,//提供的按钮
    325. this);//父组件
    326. box.setDefaultButton(QMessageBox::No);//将no设置为默认按钮
    327. //2、调用exec函数运行对话框
    328. int ret=box.exec();
    329. //3、对结果进行判断
    330. if(ret==QMessageBox::Yes)
    331. {
    332. close();
    333. }
    334. else if(ret==QMessageBox::No)
    335. {
    336. }
    337. }
    338. void zhuce::on_btn3_clicked()
    339. {
    340. //记录要录入的数据
    341. QString name=this->edit1->text();
    342. QString password=this->edit3->text();
    343. if(this->edit2->text()!=this->edit3->text()&&this->edit2->text()!=0&&this->edit3->text()!=0)
    344. {
    345. QMessageBox::information(this,"失败","请输入相同的密码");
    346. return;
    347. }
    348. //准备sql语句
    349. QString sql=QString("insert into stu_info(name,password)"
    350. "values('%1','%2')").arg(name).arg(password);
    351. //准备执行语句者
    352. QSqlQuery querry;
    353. if(!querry.exec(sql))
    354. {
    355. QMessageBox::information(this,"失败","注册失败");
    356. return;
    357. }
    358. else
    359. {
    360. QMessageBox::information(this,"成功","注册成功");
    361. //emit jump2();
    362. this->edit1->clear();
    363. this->edit2->clear();
    364. this->edit3->clear();
    365. this->close();
    366. //parentWidget()->show();
    367. }
    368. }
    369. void zhuce::jump_slot1()
    370. {
    371. this->show();
    372. }

  • 相关阅读:
    第29章_瑞萨MCU零基础入门系列教程之改进型环形缓冲区
    Visual Studio2019配置Lua及sol2框架
    [Matlab]基于LSTM+NSGA2的风光火力发电策略优化
    如何学习maya mel语言的经验分享
    一个菜鸟管理的学习和思考(一)
    python中不可变类型和可变类型
    Scala技术与架构-1
    用函数的递归来解决几道有趣的题
    【Feign请求头丢失问题】no suitable HttpMessageConverter found for response type
    Win10管理员权限怎么获取?Win10取得管理员权限的方法
  • 原文地址:https://blog.csdn.net/qq_53478460/article/details/133148860