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

ops/2025/2/12 17:36:13/

作业

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_OBJECTpublic: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.ppmy.cn/ops/157099.html

相关文章

人机环境系统智能尝试把主体、本体、客体进行了统一

Palantir代表了美国在线下大数据领域的核心竞争力&#xff08;特别是结合空天技术的军事、情报、公共安全大数据&#xff0c;是大数据时代国防和内外部安全的核心技术保障&#xff09;&#xff0c;相对于互联网不能构建真实世界完整的数据视图的虚拟特性&#xff0c;军事、金融…

react-native fetch在具有http远程服务器后端的Android设备上抛出“Network request failed“错误

问题描述&#xff1a; 在具有http远程服务器后端的Android设备上&#xff0c;使用react-native fetch时抛出"Network request failed"错误。 回答&#xff1a; "Network request failed"错误通常表示在进行网络请求时出现了问题。可能的原因包括网络连接…

day 40 复习makefile以及51单片机

1.makefile 1.流程 1.将源文件和头文件以及库分别放在src include lib中 2.在源码目录下创建一个名为makefile的文件 3.编写makefile代码。 4.make编译 2.怎么编写代码 通过定义变量 将gcc main.c tree.c queue.c -o app -lm -lpthread 转换 1.定义变量&#xff08;所有变…

Maven 构建命令详解

1. Maven 构建命令概述 Maven 是 Java 生态中最流行的构建工具之一&#xff0c;它提供了一套标准化的构建命令&#xff0c;使得开发者可以轻松管理项目的编译、测试、打包、安装和部署等任务。 本篇文章将深入解析 Maven 中最常用的构建命令&#xff0c;包括&#xff1a; mv…

路由器如何进行数据包转发?

路由器进行数据包转发的过程是网络通信的核心之一&#xff0c;主要涉及以下几个步骤&#xff1a; 接收数据包&#xff1a;当一个数据包到达路由器的一个接口时&#xff0c;它首先被暂时存储在该接口的缓冲区中。 解析目标地址&#xff1a;路由器会检查数据包中的目标IP地址。…

PostgreSQL :如何实现 rownum 的行号查询效果

在 Oracle 数据库中&#xff0c;Rownum 是非常常用的一个查询输出&#xff0c;通过 rownum 可以为结果记录增加一个类似行号的标识&#xff0c;在 PostgreSQL 数据库中缺省没有 Rownum 这个功能&#xff0c;但是可以通过分析函数来实现类似的效果。 select row_number() OVER …

想成为FPGA工程师需要学什么?主要工作内容是什么?

近年来&#xff0c;FPGA行业发展迅速&#xff0c;许多企业正在加大对FPGA人才的需求&#xff0c;特别是数字设计工程师&#xff0c;成为许多公司争抢的“香饽饽”。那么&#xff0c;想成为FPGA工程师&#xff0c;应该掌握哪些技能&#xff1f;他们的工作内容又是什么呢&#xf…

微服务架构中的事件驱动设计:使用 Kafka 和 Apache Pulsar 实现高效的事件流管理

在现代微服务架构中&#xff0c;事件驱动设计&#xff08;Event-Driven Architecture, EDA&#xff09;已成为一种流行的架构模式&#xff0c;它能够帮助处理系统中复杂的事件流和异步通信问题。事件驱动架构的核心理念是通过事件&#xff08;Event&#xff09;作为系统各个部分…