当前位置:   article > 正文

嵌入式学习Day40(Qt)_qmessagebox的 setbuttontext() 函数 无效

qmessagebox的 setbuttontext() 函数 无效

练习:将登录框进行优化

点击取消按钮,弹出一个问题对话框,询问"是否确定要退出登录",给出两个按钮,Yes|No,用户点击Yes后,关闭界面,用户点击No后,继续回到登录界面

点击登录按钮,当账号和密码验证成功,弹出信息对话框,给出信息"登录成功",给出按钮ok,用户点击ok后,发射一个自定义信号后关闭登录界面

当账号和密码验证失败,则弹出错误对话框,给出信息"账号和密码不匹配,是否重新登录",并给出两个按钮Yes|No,用户点击Yes后,回到登录界面,并清空密码输入框中的内容。用户点击No后,直接关闭登录界面

要求:至少使用一个基于属性版本和一个基于静态成员函数版本的消息对话框

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QDebug>
  5. #include <QIcon>
  6. #include <QLabel>
  7. #include <QLineEdit>
  8. #include <QPushButton>
  9. #include <iostream>
  10. #include <QMessageBox>
  11. QT_BEGIN_NAMESPACE
  12. namespace Ui { class Widget; }
  13. QT_END_NAMESPACE
  14. class Widget : public QWidget
  15. {
  16. Q_OBJECT
  17. signals:
  18. //声明一个登录信号
  19. void loginSuccess();
  20. private slots:
  21. void on_loginButton_clicked();
  22. void on_cancelButton_clicked();
  23. private:
  24. // 定义界面中的组件
  25. QLineEdit *edt_user;
  26. QLineEdit *edt_pwd;
  27. public:
  28. Widget(QWidget *parent = nullptr);
  29. ~Widget();
  30. private:
  31. Ui::Widget *ui;
  32. };
  33. #endif // WIDGET_H

源文件

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::Widget)
  6. {
  7. ui->setupUi(this);
  8. //设置固定尺寸
  9. this->setFixedSize(555,375);
  10. //设置窗口标题
  11. this->setWindowTitle("QQ");
  12. //设置窗口图标
  13. this->setWindowIcon(QIcon("E:\\c++\\qq.jpg"));
  14. //设置背景色
  15. this->setBackgroundRole(QPalette::Light);
  16. //设置允许填充背景
  17. this->setAutoFillBackground(true);
  18. //定义一个标签,做上半背景
  19. QLabel *lab_bgp = new QLabel(this);
  20. //设置尺寸
  21. lab_bgp->resize(555,150);
  22. //设置透明度
  23. lab_bgp->setWindowOpacity(0.3);
  24. //设置按钮颜色
  25. lab_bgp->setStyleSheet("background-color:rgb(137,207,240)");
  26. //定义一个标签头像
  27. QLabel *lab_hp = new QLabel(this);
  28. //设置尺寸
  29. lab_hp->resize(80,80);
  30. //设置头像图标
  31. lab_hp->setPixmap(QPixmap("E:\\c++\\qq1.png"));
  32. lab_hp->move(225,78);
  33. //设置填充格式:true-自适应大小
  34. lab_hp->setScaledContents(true);
  35. //定义一个账号标签
  36. QLabel *lab_user = new QLabel("账户:",this);
  37. //设置尺寸
  38. lab_user->resize(19,23);
  39. //设置头像图标
  40. lab_user->setPixmap(QPixmap(":/new/prefix1/icon/userName.jpg"));
  41. lab_user->move(120,185);
  42. //设置填充格式:true-自适应大小
  43. lab_user->setScaledContents(true);
  44. //定义一个密码标签
  45. QLabel *lab_pwd = new QLabel("密码:",this);
  46. //设置尺寸
  47. lab_pwd->resize(19,22);
  48. //设置头像图标
  49. lab_pwd->setPixmap(QPixmap(":/new/prefix1/icon/passwd.jpg"));
  50. lab_pwd->move(120,240);
  51. //设置填充格式:true-自适应大小
  52. lab_pwd->setScaledContents(true);
  53. //定义账号的行编辑
  54. edt_user = new QLineEdit(this);
  55. //设置尺寸和位置
  56. edt_user->resize(280,45);
  57. edt_user->move(140,170);
  58. //设置默认值
  59. edt_user->setPlaceholderText("账户");
  60. //设置边框只留底行
  61. edt_user->setStyleSheet("border: none; border-bottom: 1px solid black;");
  62. //定义密码的行编辑
  63. edt_pwd = new QLineEdit(this);
  64. //设置尺寸和位置
  65. edt_pwd->resize(280,45);
  66. edt_pwd->move(140,230);
  67. //设置默认值
  68. edt_pwd->setPlaceholderText("密码");
  69. //设置密码行为回显模式
  70. edt_pwd->setEchoMode(QLineEdit::Password); //设置密文模式
  71. //设置边框只留底行
  72. edt_pwd->setStyleSheet("border: none; border-bottom: 1px solid black;");
  73. //定义登录按钮
  74. QPushButton *btn_login = new QPushButton("登录",this);
  75. //设置登录图片
  76. btn_login->setIcon(QIcon(":/new/prefix1/icon/login.png"));
  77. //设置尺寸、位置和颜色
  78. btn_login->resize(80,40);
  79. btn_login->move(150,300);
  80. btn_login->setStyleSheet("background-color:rgb(19,183,246)");
  81. //定义登录按钮
  82. QPushButton *btn_cancel = new QPushButton("取消",this);
  83. //设置取消图片
  84. btn_cancel->setIcon(QIcon(":/new/prefix1/icon/cancel.png"));
  85. //设置尺寸、位置和颜色
  86. btn_cancel->resize(80,40);
  87. btn_cancel->move(310,300);
  88. btn_cancel->setStyleSheet("background-color:red");
  89. // 将登录按钮和取消按钮与相应的槽函数关联
  90. connect(btn_login, &QPushButton::clicked, this, &Widget::on_loginButton_clicked);
  91. connect(btn_cancel, &QPushButton::clicked, this, &Widget::on_cancelButton_clicked);
  92. }
  93. // 登录按钮被点击时执行的槽函数
  94. void Widget::on_loginButton_clicked()
  95. {
  96. //获取账号和密码输入框中的文本
  97. QString intputAccout=edt_user->text();
  98. QString intputPassword=edt_pwd->text();
  99. // 验证账号和密码是否匹配
  100. if (intputAccout == "admin" && intputPassword == "123456")
  101. {
  102. // 登录成功,弹出信息对话框并发射登录成功信号
  103. QMessageBox::information(this, "信息", "登录成功");
  104. emit loginSuccess();
  105. } else {
  106. // 登录失败,弹出错误对话框并询问是否重新登录
  107. QMessageBox::StandardButton result = QMessageBox::warning(this, "错误", "密码错误,请重试!", QMessageBox::Yes | QMessageBox::No);
  108. if (result == QMessageBox::Yes) {
  109. // 重新登录,清空密码输入框
  110. edt_pwd->clear();
  111. } else {
  112. // 不重新登录,关闭登录窗口
  113. close();
  114. }
  115. }
  116. }
  117. // 取消按钮被点击时执行的槽函数
  118. void Widget::on_cancelButton_clicked()
  119. {
  120. // 弹出问题对话框,询问是否确定要退出登录
  121. QMessageBox box(QMessageBox::Question, "问题", "是否退出?", QMessageBox::Yes | QMessageBox::No);
  122. box.setButtonText(QMessageBox::Yes, "Yes");
  123. box.setButtonText(QMessageBox::No, "No");
  124. // 将 Yes 和 No 按钮与相应的槽函数关联
  125. if (box.exec() == QMessageBox::Yes) {
  126. close();
  127. }
  128. }
  129. Widget::~Widget()
  130. {
  131. delete ui;
  132. }

 

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

闽ICP备14008679号