第二章:QT核心机制(一)

news/2025/2/8 13:32:02 标签: qt, 数据库, 开发语言

作业

1> 手动将登录项目实现,不要使用拖拽编程

并且,当点击登录按钮时,后台会判断账号和密码是否相等,如果相等给出登录成功的提示,并且关闭当前界面,发射一个跳转信号,如果登录失败,则给出登录失败的提示,并清空密码框

当点击取消按钮时,直接关闭当前登录框

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QPainter>
#include <QBitmap>
#include <QPainterPath>
#include <QDebug>
#include <QApplication>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void onLoginButtonClicked();
    void onCancelButtonClicked();

private:
    void setupWindow();
    void setupWidgets();
    void setupPlaceholderText();
    bool validateLogin();
    QLineEdit *accountEdit;
    QLineEdit *passwordEdit;
    QLabel *accountLabel;
    QLabel *passwordLabel;
    QPushButton *loginButton;
    QPushButton *cancelButton;
    QCheckBox *rememberCheckBox;
    QCheckBox *autoLoginCheckBox;
    QLabel *avatarLabel;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 设置窗口固定尺寸
    setFixedSize(600, 800);
    // 设置窗口标题
    setWindowTitle("QQ");
    setWindowIcon(QIcon("E:/a-U盘备份/a资料/证件/头像.jpg"));
    //设置窗口样式表
    setWindowOpacity(0.9);
    setStyleSheet("background-color:rgb(74, 255, 219);");  //设置背景色

    // 头像标签
    avatarLabel = new QLabel(this);
    avatarLabel->setGeometry(230, 150, 151, 151);

    QPixmap originalPixmap("E:/a-U盘备份/a资料/证件/头像.jpg");
    if (!originalPixmap.isNull()) {
        QPixmap circularPixmap(avatarLabel->size());
        circularPixmap.fill(Qt::transparent);

        QPainter painter(&circularPixmap);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(Qt::NoPen);
        painter.drawRoundedRect(0, 0, circularPixmap.width(), circularPixmap.height(),
                                circularPixmap.width() / 2, circularPixmap.width() / 2);
        QPainterPath path;
        path.addRoundedRect(0, 0, circularPixmap.width(), circularPixmap.height(), circularPixmap.width() / 2, circularPixmap.width() / 2);
        painter.setClipPath(path);
        painter.drawPixmap(0, 0, originalPixmap.scaled(circularPixmap.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));

        avatarLabel->setPixmap(circularPixmap);
    }

    // 账号标签
    accountLabel = new QLabel("账号:", this);
    accountLabel->setGeometry(60, 480, 81, 50);
    // 账号输入框
    accountEdit = new QLineEdit(this);
    accountEdit->setGeometry(160, 480, 400, 50);
    accountEdit->setAlignment(Qt::AlignCenter);
    accountEdit->setPlaceholderText("请输入账号");

    // 密码标签
    passwordLabel = new QLabel("密码:", this);
    passwordLabel->setGeometry(60, 550, 81, 50);
    // 密码输入框
    passwordEdit = new QLineEdit(this);
    passwordEdit->setGeometry(160, 550, 400, 50);
    passwordEdit->setEchoMode(QLineEdit::Password);
    passwordEdit->setAlignment(Qt::AlignCenter);
    passwordEdit->setPlaceholderText("请输入密码");

    // 登录按钮
    loginButton = new QPushButton("登录", this);
    loginButton->setGeometry(130, 660, 150, 50);

    // 取消按钮
    cancelButton = new QPushButton("取消", this);
    cancelButton->setGeometry(330, 660, 150, 50);

    // 记住密码复选框
    rememberCheckBox = new QCheckBox("记住密码", this);
    rememberCheckBox->setGeometry(120, 620, 200, 30);

    // 自动登录复选框
    autoLoginCheckBox = new QCheckBox("自动登录", this);
    autoLoginCheckBox->setGeometry(330, 620, 200, 30);

    // 连接按钮点击信号到对应的槽函数
    bool connectResult1 = connect(loginButton, &QPushButton::clicked, this, &MainWindow::onLoginButtonClicked);
    bool connectResult2 = connect(cancelButton, &QPushButton::clicked, this, &MainWindow::onCancelButtonClicked);
    if (!connectResult1 ||!connectResult2) {
        qDebug() << "信号 - 槽连接失败";
    }
}

MainWindow::~MainWindow()
{
}

bool MainWindow::validateLogin()
{
    QString account = accountEdit->text();
    QString password = passwordEdit->text();
    // 这里是简单的校验逻辑,可根据实际需求修改
    return account == "123456" && password == "654321";
}

void MainWindow::onLoginButtonClicked()
{
    if (validateLogin()) {
        qDebug() << "登录成功";
        qApp->quit();
    } else {
        qDebug() << "登录失败";
        passwordEdit->clear();
    }
}

void MainWindow::onCancelButtonClicked()
{
    close();
}

2> 思维导图


http://www.niftyadmin.cn/n/5844895.html

相关文章

数据库操作与数据管理——Rust 与 SQLite 的集成

第六章&#xff1a;数据库操作与数据管理 第一节&#xff1a;Rust 与 SQLite 的集成 在本节中&#xff0c;我们将深入探讨如何在 Rust 中使用 SQLite 数据库&#xff0c;涵盖从基本的 CRUD 操作到事务处理、数据模型的构建、性能优化以及安全性考虑等方面。SQLite 是一个轻量…

ES6 Set 数据结构用法总结

1. Set 基本概念 Set 是 ES6 提供的新的数据结构&#xff0c;类似于数组&#xff0c;但成员的值都是唯一的&#xff0c;没有重复的值。Set 本身是一个构造函数&#xff0c;用来生成 Set 数据结构。 1.1 基本用法 // 创建一个空Set const set new Set();// 创建一个带有初始…

Elasticsearch 生产集群部署终极方案

Elasticsearch 集群部署 1.集群部署1.1 新增用户1.2 优化操作系统1.3 JDK1.4 elasticsearch1.5 开机自启动 2.安全认证功能2.1 生成CA证书2.2 生成密钥2.3 上传至其他节点2.4 修改属主、属组2.5 配置文件添加参数2.6 各节点添加密钥库密码2.7 设置用户密码 1.集群部署 1.1 新增…

1Panel应用推荐:WordPress开源博客软件和内容管理系统

1Panel&#xff08;github.com/1Panel-dev/1Panel&#xff09;是一款现代化、开源的Linux服务器运维管理面板&#xff0c;它致力于通过开源的方式&#xff0c;帮助用户简化建站与运维管理流程。为了方便广大用户快捷安装部署相关软件应用&#xff0c;1Panel特别开通应用商店&am…

【理论知识】 2D 卷积、3D 卷积与 3D 池化

摘要 卷积神经网络&#xff08;Convolutional Neural Networks, CNNs&#xff09;在计算机视觉、视频处理和医学影像分析等领域取得了显著的成功。卷积操作作为CNN的核心&#xff0c;主要包括二维卷积&#xff08;2D Convolution&#xff09;、三维卷积&#xff08;3D Convolu…

探索.NET Core的神秘侦探:CallerMemberName、FilePath、LineNumber

一、引言 在.NET Core 开发的广袤天地中&#xff0c;调试与日志记录堪称开发者手中的两大法宝&#xff0c;它们对于保障程序的稳定运行、快速定位并解决问题起着举足轻重的作用。想象一下&#xff0c;当你的应用程序在生产环境中突然出现异常&#xff0c;没有调试和日志记录的…

【DeepSeek论文精读】2. DeepSeek LLM:以长期主义扩展开源语言模型

欢迎关注[【youcans的AGI学习笔记】](https://blog.csdn.net/youcans/category_12244543.html&#xff09;原创作品 【DeepSeek论文精读】1. 从 DeepSeek LLM 到 DeepSeek R1 【DeepSeek论文精读】2. DeepSeek LLM&#xff1a;以长期主义扩展开源语言模型 【DeepSeek论文精读】…

Mac: docker安装以后报错Command not found: docker

文章目录 前言解决办法&#xff08;新的&#xff09;解决步骤&#xff08;原来的&#xff09;不推荐总结 前言 ​本操作参考 http://blog.csdn.net/enhenglhm/article/details/137955756 原作者&#xff0c;更详细请&#xff0c;查看详细内容请关注原作者。 一般&#xff0c;…