作业
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> 思维导图