qt QMessageBox案例,2024.10.8

devtools/2024/10/9 2:27:15/

当用户点击取消按钮,弹出问题对话框,询问是否要确定退出登录,并提供两个按钮,yes|No,如果用户点击的Yes,则关闭对话框,如果用户点击的No,则继续登录

当用户点击的登录按钮,进行账号和密码的匹配,如果匹配成功,则弹出信息对话框,给出信息为,登录成功,并给出一个确定按钮,当用户点击该按钮后,关闭登录界面,弹出另一个界面

当账号和密码不匹配是,给出错误对话框,给出信息为账号和密码不匹配,是否重新登录,并提供两个按钮 Yes|No,如果用户点击了Yes,则清空密码框后,继续登录。如果用户点击的取消,则关闭登录界面

要求:静态成员函数版本和对象版本各至少实现一个

1.main.cpp

#include "loginwindow.h"
#include <QApplication>int main(int argc, char *argv[]) {QApplication app(argc, argv);LoginWindow window;window.resize(300, 150);window.setWindowTitle("登录");app.setWindowIcon(QIcon(":/new/prefix1/pit/Character_sheet.png"));window.show();return app.exec();
}

2.loginwindow.h

#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QMessageBox>class LoginWindow : public  QWidget {Q_OBJECTpublic:LoginWindow(QWidget *parent = nullptr); // 构造函数private slots:void onLoginButtonClicked();   // 登录按钮点击处理void onCancelButtonClicked();  // 取消按钮点击处理private:QLineEdit *usernameEdit;      // 用户名输入框QLineEdit *passwordEdit;      // 密码输入框QPushButton *loginButton;      // 登录按钮QPushButton *cancelButton;     // 取消按钮// 静态成员函数版本static void onStaticCancelButtonClicked(QApplication *app);
};#endif // LOGINWINDOW_H

3.loginwindow.cpp

#include "loginwindow.h"
#include "successwindow.h"
#include <QApplication>
#include <QMessageBox>// 构造函数实现
LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent) {// 创建布局和控件QVBoxLayout *layout = new QVBoxLayout(this);usernameEdit = new QLineEdit(this);usernameEdit->setPlaceholderText("账号");passwordEdit = new QLineEdit(this);passwordEdit->setEchoMode(QLineEdit::Password);passwordEdit->setPlaceholderText("密码");loginButton = new QPushButton("登录", this);cancelButton = new QPushButton("取消", this);// 添加控件到布局layout->addWidget(usernameEdit);layout->addWidget(passwordEdit);layout->addWidget(loginButton);layout->addWidget(cancelButton);// 信号与槽连接connect(loginButton, &QPushButton::clicked, this, &LoginWindow::onLoginButtonClicked);connect(cancelButton, &QPushButton::clicked, this, &LoginWindow::onCancelButtonClicked);
}// 登录按钮点击处理
void LoginWindow::onLoginButtonClicked() {QString correctUsername = "123";QString correctPassword = "123";if (usernameEdit->text() == correctUsername && passwordEdit->text() == correctPassword) {QMessageBox::information(this, "登录成功", "欢迎进入系统!", QMessageBox::Ok);this->close();  SuccessWindow *successWindow = new SuccessWindow();successWindow->show();} else {QMessageBox::StandardButton reply;reply = QMessageBox::question(this, "登录失败", "账号和密码不匹配,是否重新登录?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {// 清空密码框passwordEdit->clear();} else {this->close(); }}
}// 取消按钮点击处理
void LoginWindow::onCancelButtonClicked() {// 弹出确认对话框QMessageBox::StandardButton reply;reply = QMessageBox::question(this, "确认退出", "您确定要退出登录吗?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {this->close();}
}// 静态成员函数版本
void LoginWindow::onStaticCancelButtonClicked(QApplication *app) {QMessageBox::StandardButton reply;reply = QMessageBox::question(nullptr, "确认退出", "您确定要退出登录吗?", QMessageBox::Yes | QMessageBox::No);if (reply == QMessageBox::Yes) {app->quit(); }
}

4.successwindow.h

#ifndef SUCCESSWINDOW_H
#define SUCCESSWINDOW_H#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>class SuccessWindow : public QWidget {Q_OBJECTpublic:SuccessWindow(QWidget *parent = nullptr);
};#endif 

5.successwindow.cpp

#include "successwindow.h"// 构造函数实现
SuccessWindow::SuccessWindow(QWidget *parent) : QWidget(parent) {QVBoxLayout *layout = new QVBoxLayout(this);QLabel *label = new QLabel("登录成功!欢迎使用本系统。", this);layout->addWidget(label);setLayout(layout);setWindowTitle("成功");resize(300, 100);
}


http://www.ppmy.cn/devtools/123147.html

相关文章

六、Drf限流组件

六、限流组件 限制某个视图在某个时间段内被同一个用户访问的次数 6.1限流组件的简单应用 1&#xff09;安装django-redis pip3 install django-redis2)在settings.py中注册cache #缓存数据库redis配置 CACHES{"default":{"BACKEND":"django_red…

【word脚注】双栏设置word脚注,脚注仅位于左栏,右栏不留白

【word脚注】双栏设置word脚注&#xff0c;脚注仅位于左栏&#xff0c;右栏不留白 调整前效果解决方法调整后效果参考文献 调整前效果 调整前&#xff1a;脚注位于左下角&#xff0c;但右栏与左栏内容对其&#xff0c;未填充右下角的空白区域 解决方法 备份源文件复制脚注内…

【堆排】为何使用向下调整法建堆比向上调整法建堆更好呢?

文章目录 前言一、堆排代码一、计算使用向上调整法建堆的时间复杂度二、计算使用向下调整法插入的时间复杂度总结 前言 在博主的上一篇博客堆排(链接在这里点击即可)的总结中提出啦使用向下调整法建堆比使用向上调整法建堆更好&#xff0c;是因为使用向上调整法建堆的时间复杂…

SimpleFoc以及SVPWM学习补充记录

SimpleFoc SimpleFOC移植STM32&#xff08;一&#xff09;—— 简介 FOC控制的过程是这样的&#xff1a; 对电机三相电流进行采样得到 Ia,Ib,Ic。将 Ia,Ib,Ic 经过Clark变换得到 I_alpha I_beta。将 I_alpha I_beta 经过Park变换得到 Id,Iq。计算 Id,Iq 和其设定值 Id_ref 和…

gbase8s数据库实现黑白名单的几种方案

1、借用操作系统的黑白名单 2、使用数据库 TRUSTED CONTEXT 机制 CREATE TRUSTED CONTEXT tcx1USER rootATTRIBUTES (ADDRESS 172.16.39.162)ATTRIBUTES (ADDRESS 172.16.39.163)ENABLEWITH USE FOR wangyx WITHOUT AUTHENTICATION; 如上创建 可信任上下文对象 tcx1 在 jdb…

Java访问器方法和更改器方法

一.访问器方法 1.访问器方法的定义和用途 访问器方法&#xff0c;通常也称为getter方法&#xff0c;是一种在面向对象编程中用于从类的外部访问私有字段值的特殊方法。这些方法的设计目的是为了提供对类内部状态的受限访问&#xff0c;同时保持类的封装性。通过使用访问器方法&…

人力资源管理软件推荐,8款顶尖产品盘点

多款人力资源管理软件各具特色&#xff0c;适用于不同规模企业。ZohoPeople全面、安全、智能&#xff0c;Workday云端综合&#xff0c;OracleHCMCloud适合跨国企业&#xff0c;Moka、北森、薪人薪事、欢雀HR等各有优势&#xff0c;企业应根据需求选择并试用。 一、Zoho People …

大数据算法的思维

大数据算法的分类 一、分类算法 1. 决策树算法&#xff1a;通过构建树状结构&#xff0c;对数据进行分类。例如 ID3、C4.5 和 CART 算法&#xff0c;它们根据不同的特征选择标准进行分支划分&#xff0c;最终形成一颗能够对新数据进行分类的决策树。 2. 支持向量机&#xff08…