代码拉取完成,页面将自动刷新
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, user(new UserWindow())
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->mainWidget->setMouseTracking(true);
QTimer *delay = new QTimer(this);
connect(delay, &QTimer::timeout, this, [this](){init();});
delay->setSingleShot(true); // 只触发一次
delay->start(10); // 让构造函数执行完后再进行初始化
}
void MainWindow::init() {
QPainterPath path;
path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1);
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
QString mainStyle;
ui->mainWidget->setObjectName("mainWidget");
mainStyle = "QWidget#mainWidget{background-color:" + mainBackGround.name() + QString::asprintf(";border-radius:%dpx", cornerRadius) + "}";
ui->mainWidget->setStyleSheet(mainStyle);
windowShadow = new QGraphicsDropShadowEffect(this);
windowShadow->setBlurRadius(30);
windowShadow->setColor(QColor(0, 0, 0));
windowShadow->setOffset(0, 0);
ui->mainWidget->setGraphicsEffect(windowShadow);
border = new QWidget(this);
border->move(ui->mainWidget->pos() - QPoint(1, 1));
border->resize(ui->mainWidget->size() + QSize(2, 2));
QString borderStyle = "background-color:#00FFFFFF;border:1.5px solid #686868; border-radius:20px";
border->setStyleSheet(borderStyle);
border->setAttribute(Qt::WA_TransparentForMouseEvents);
border->show();
// 创建用户登录界面
user_login = new LoginWidget(ui->userWidget);
user_login->show();
// 创建用户注册界面(隐藏)
user_register = new RegisterWidget(ui->userWidget);
user_register->hide();
// 创建用户找回密码界面(隐藏)
user_findpasswd = new FindPasswd(ui->userWidget);
user_findpasswd->hide();
// 用户按下登录按钮
connect(user_login, &LoginWidget::notify_register, this, [this](){
if (!checkOpen()) return;
user_login->hide();
user_register->show();
});
// 用户按下忘记密码按钮
connect(user_login, &LoginWidget::notify_forget, this, [this](){
if (!checkOpen()) return;
user_login->hide();
user_findpasswd->show();
});
// 注册用户按下返回按钮
connect(user_register, &RegisterWidget::notify_back, this, [this](){
if (!checkOpen()) return;
user_register->hide();
user_login->show();
});
// 找回用户按下返回按钮
connect(user_findpasswd, &FindPasswd::notify_back, this, [this](){
if (!checkOpen()) return;
user_findpasswd->hide();
user_login->show();
});
// 用户按下登录按钮
connect(user_login, &LoginWidget::notify_login, this, &MainWindow::deal_with_login);
// 用户按下注册按钮
connect(user_register, &RegisterWidget::notify_register, this, &MainWindow::deal_with_register);
// 用户按下更改密码按钮
connect(user_findpasswd, &FindPasswd::notify_find, this, &MainWindow::deal_with_findpasswd);
}
// 检查用户网络连接
bool MainWindow::checkOpen() {
if (!user_login->isOpen() || !user_register->isOpen() || !user_findpasswd->isOpen()) {
QMessageBox::critical(this, QString::fromLocal8Bit("注意"),
QString::fromLocal8Bit("请连接网络!"));
return false;
}
return true;
}
// 处理登录界面返回值
void MainWindow::deal_with_login(LoginWidget::STATE state) {
if (!checkOpen()) return;
switch (state) {
case LoginWidget::LOGIN_YES:
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("登录成功!"));
break;
}
case LoginWidget::LOGIN_USER:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名不存在!"));
break;
}
case LoginWidget::LOGIN_PASS:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("密码错误!"));
break;
}
}
if (state == LoginWidget::LOGIN_YES) {
// 将用户信息记录到全局数据结构中
GlobalData::globalInstance()->setUserName(user_login->username);
GlobalData::globalInstance()->setPassWord(user_login->password);
this->hide(); // 隐藏当前窗口
user->show(); // 显示主窗口
}
}
// 处理注册界面返回值
void MainWindow::deal_with_register(RegisterWidget::STATE state) {
if (!checkOpen()) return;
switch (state) {
case RegisterWidget::REGISTER_YES:
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("注册成功!"));
user_register->hide();
user_login->show();
break;
}
case RegisterWidget::REGISTER_CODE:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("验证码错误!"));
break;
}
case RegisterWidget::REGISTER_PASS_DIFF:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("密码不一致!"));
break;
}
case RegisterWidget::REGISTER_PASS_SHORT:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("密码过短!"));
break;
}
case RegisterWidget::REGISTER_USER:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名已存在!"));
break;
}
case RegisterWidget::REGISTER_EMPTY:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名不能为空!"));
break;
}
case RegisterWidget::REGISTER_EMAIL_ERROR:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("邮箱格式错误!"));
break;
}
case RegisterWidget::REGISTER_EMAIL_SEND:
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("验证码已发送!"));
break;
}
case RegisterWidget::REGISTER_FORMAT:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名格式错误(只含大小写字母)!"));
break;
}
}
}
// 处理找回密码界面返回值
void MainWindow::deal_with_findpasswd(FindPasswd::STATE state) {
if (!checkOpen()) return;
switch (state) {
case FindPasswd::FIND_YES:
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("密码更改成功!"));
user_findpasswd->hide();
user_login->show();
break;
}
case FindPasswd::FIND_CODE:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("验证码错误!"));
break;
}
case FindPasswd::FIND_USER:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名不存在!"));
break;
}
case FindPasswd::FIND_EMAIL_SEND:
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("验证码已发送!"));
break;
}
case FindPasswd::FIND_EMAIL_ERROR:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("邮箱格式错误!"));
break;
}
case FindPasswd::FIND_EMPTY:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名不能为空!"));
break;
}
case FindPasswd::FIND_PASS_DIFF:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("密码不一致!"));
break;
}
case FindPasswd::FIND_PASS_SHORT:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("密码过短!"));
break;
}
case FindPasswd::FIND_FORMAT:
{
QMessageBox::warning(this, QString::fromLocal8Bit("警告"),
QString::fromLocal8Bit("用户名格式错误(只含大小写字母)!"));
break;
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::controlWindowScale(){
if(!maximized){
lastGeometry = this->frameGeometry();
windowShadow->setEnabled(false);
ui->verticalLayout->setContentsMargins(0, 0, 0, 0);
border->hide();
QString mainStyle = "QWidget#mainWidget{background-color:" + mainBackGround.name() + ";border-radius:0px;}";
ui->mainWidget->setStyleSheet(mainStyle);
this->showMaximized();
maximized = true;
QPainterPath path;
path.addRect(ui->mainWidget->rect());
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
}
else{
ui->verticalLayout->setContentsMargins(30, 30, 30, 30);
this->showNormal();
QString mainStyle = "QWidget#mainWidget{background-color:" + mainBackGround.name() + QString::asprintf(";border-radius:%dpx", cornerRadius) + "}";
ui->mainWidget->setStyleSheet(mainStyle);
QPainterPath path;
path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1);
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
border->show();
windowShadow->setEnabled(true);
this->resize(lastGeometry.width(), lastGeometry.height());
this->move(lastGeometry.x(), lastGeometry.y());
maximized = false;
}
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
mousePressed = true;
lastPos = event->globalPos() - this->frameGeometry().topLeft();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
if(event->buttons() == Qt::NoButton)
mousePressed = false;
if(!mousePressed){
mouseState = 0;
if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x()) < 5)
mouseState |= AT_LEFT;
if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y()) < 5)
mouseState |= AT_TOP;
if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x() - ui->mainWidget->width()) < 5)
mouseState |= AT_RIGHT;
if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y() - ui->mainWidget->height()) < 5)
mouseState |= AT_BOTTOM;
if(mouseState == AT_TOP_LEFT || mouseState == AT_BOTTOM_RIGHT)
setCursor(Qt::SizeFDiagCursor);
else if(mouseState == AT_TOP_RIGHT || mouseState == AT_BOTTOM_LEFT)
setCursor(Qt::SizeBDiagCursor);
else if(mouseState & (AT_LEFT | AT_RIGHT))
setCursor(Qt::SizeHorCursor);
else if(mouseState & (AT_TOP | AT_BOTTOM))
setCursor(Qt::SizeVerCursor);
else
unsetCursor();
}
else{
if(mouseState == 0){
if(maximized){
qreal wRatio = (double)event->pos().x() / (double)ui->mainWidget->width();
controlWindowScale();
this->move(QPoint(event->globalPos().x() - ui->mainWidget->width() * wRatio, -30));
lastPos = QPoint(ui->mainWidget->width() * wRatio, event->pos().y());
}
else
this->move(event->globalPos() - lastPos);
}
else{
QPoint d = event->globalPos() - frameGeometry().topLeft() - lastPos;
if(mouseState & AT_LEFT){
this->move(this->frameGeometry().x() + d.x(), this->frameGeometry().y());
this->resize(this->width() - d.x(), this->height());
}
if(mouseState & AT_RIGHT){
this->resize(this->width() + d.x(), this->height());
}
if(mouseState & AT_TOP){
this->move(this->frameGeometry().x(), this->frameGeometry().y() + d.y());
this->resize(this->width(), this->height() - d.y());
}
if(mouseState & AT_BOTTOM){
this->resize(this->width(), this->height() + d.y());
}
}
lastPos = event->globalPos() - this->frameGeometry().topLeft();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event) {
Q_UNUSED(event);
mousePressed = false;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。