QT登录界面系统
- 1、前言
- 1.1创建账号,密码标签
- 1.2创建账号,密码输入框
- 1.3创建登录按钮
- 1.4创建一个函数设置控件位置和大小
- 1.5登录验证
- 1.6事件过滤器
- 1.7密码定时器
- 1.8创建控件样式
- 2、头文件和.cpp文件
1、前言
前言:设计一个登录界面,输入密码时过了1秒会变成*或者输入下一个密码,上一个直
接变成*,不用ui设计师,纯靠代码设计,在这里做个总结,可以分享给别人参考,也可
以巩固自己的学习。 |
1.1创建账号,密码标签
QLabel* createLabel(const QString &text, int offsetX, int offsetY);
QLabel* MainWindow::createLabel(const QString &text, int offsetX, int offsetY)
{
QLabel *label = new QLabel(text, this);
setWindowGeometry(label, offsetX, offsetY);
return label;
}
accountNumberLabel = createLabel("账号", 50, 125);
passWorldLabel = createLabel("密码", 50, 80);
1.2创建账号,密码输入框
QLineEdit* createLineEdit(int width, int offsetX, int offsetY);
QLineEdit* MainWindow::createLineEdit(int width, int offsetX, int offsetY)
{
QLineEdit *lineEdit = new QLineEdit(this);
setWindowGeometry(lineEdit, offsetX, offsetY, width - lineEdit->width());
return lineEdit;
}
accountNumberLineEdit = createLineEdit(200, 10, 125);
passWorldLineEdit = createLineEdit(200, 10, 80);
passWorldLineEdit->setEchoMode(QLineEdit::Normal);
passWorldLineEdit->installEventFilter(this);
1.3创建登录按钮
void createLoginButton(int offsetX, int offsetY);
void MainWindow::createLoginButton(int offsetX, int offsetY)
{
login_button = new QPushButton("登录", this);
setWindowGeometry(login_button, offsetX, offsetY, -10, 10);
}
createLoginButton(-210, 105);
1.4创建一个函数设置控件位置和大小
void setWindowGeometry(QWidget *window, int offsetX, int offsetY, int widthW=0, int widthH=0);
void MainWindow::setWindowGeometry(QWidget *window, int offsetX, int offsetY, int widthW, int widthH)
{
int x = (this->width() - window->width()) / 2 - offsetX;
int y = (this->height() - window->height()) / 2 - offsetY;
window->setGeometry(x, y, window->width() + widthW,window->height() + widthH);
}
1.5登录验证
void loginButton();
connect(login_button, &QPushButton::clicked, this, &MainWindow::loginButton);
void MainWindow::loginButton()
{
QString account = accountNumberLineEdit->text();
QString password = visiblePassworld;
if (account == "admin" && password == "123456") {
QMessageBox::information(this, "登录成功", "欢迎回来!");
} else {
QMessageBox::warning(this, "登录失败", "账号或密码错误");
}
}
1.6事件过滤器
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == passWorldLineEdit && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Backspace) {
if (!visiblePassworld.isEmpty()) {
visiblePassworld.chop(1);
QString displayText = visiblePassworld.isEmpty() ?
"" :
QString(visiblePassworld.length() - 1, '*') + visiblePassworld.right(1);
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
passWorldTimer->start(1000);
}
return true;
}
else if (!keyEvent->text().isEmpty()) {
visiblePassworld += keyEvent->text();
QString displayText = (visiblePassworld.length() > 1) ?
QString(visiblePassworld.length() - 1, '*') + visiblePassworld.right(1) :
visiblePassworld;
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
passWorldTimer->start(1000);
return true;
}
}
return QMainWindow::eventFilter(watched, event);
}
1.7密码定时器
void passWorld_startTimer();
connect(login_button, &QPushButton::clicked, this, &MainWindow::loginButton);
void MainWindow::passWorld_startTimer()
{
if (!visiblePassworld.isEmpty()) {
QString displayText = QString(visiblePassworld.length(), '*');
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
}
}
1.8创建控件样式
void applyStyles();
void MainWindow::applyStyles()
{
QString styleSheet =
"QMainWindow {"
"background-color: #F8F9FA;"
"}"
"QLabel {"
"color: #495057;"
"font-size: 14px;"
"font-weight: 500;"
"padding: 6px 0;"
"}"
"QLineEdit {"
"background-color: #FFFFFF;"
"color: #212529;"
"border: 1px solid #CED4DA;"
"border-radius: 6px;"
"padding: 8px 12px;"
"font-size: 14px;"
"selection-background-color: #E9ECEF;"
"}"
"QLineEdit:focus {"
"border: 2px solid #4DABF7;"
"background-color: #F8F9FA;"
"color: #212529;"
"}"
"QPushButton {"
"background-color: #4DABF7;"
"color: #FFFFFF;"
"border: none;"
"border-radius: 8px;"
"padding: 10px 24px;"
"font-size: 14px;"
"font-weight: 600;"
"transition: all 0.3s ease;"
"box-shadow: 0 2px 4px rgba(0,0,0,0.1);"
"}"
"QPushButton:hover {"
"background-color: #339AF0;"
"box-shadow: 0 4px 6px rgba(0,0,0,0.15);"
"transform: translateY(-1px);"
"}"
"QPushButton:pressed {"
"background-color: #228BE6;"
"box-shadow: 0 1px 2px rgba(0,0,0,0.1);"
"transform: translateY(0);"
"}"
"QPushButton:disabled {"
"background-color: #E9ECEF;"
"color: #ADB5BD;"
"}";
this->setStyleSheet(styleSheet);
}
2、头文件和.cpp文件
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
private slots:
void loginButton();
void passWorld_startTimer();
private:
Ui::MainWindow *ui;
QLabel *accountNumberLabel;
QLabel *passWorldLabel;
QLineEdit *accountNumberLineEdit;
QLineEdit *passWorldLineEdit;
QPushButton *login_button;
QTimer *passWorldTimer;
QString visiblePassworld;
void setWindowGeometry(QWidget *control, int offsetX, int offsetY, int widthW=0, int widthH=0);
QLabel* createLabel(const QString &text, int offsetX, int offsetY);
QLineEdit* createLineEdit(int width, int offsetX, int offsetY);
void createLoginButton(int offsetX, int offsetY);
void applyStyles();
};
#endif
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QKeyEvent>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, accountNumberLabel(nullptr)
, passWorldLabel(nullptr)
, passWorldTimer(new QTimer(this))
, visiblePassworld("")
{
ui->setupUi(this);
accountNumberLabel = createLabel("账号", 50, 125);
passWorldLabel = createLabel("密码", 50, 80);
accountNumberLineEdit = createLineEdit(200, 10, 125);
passWorldLineEdit = createLineEdit(200, 10, 80);
passWorldLineEdit->setEchoMode(QLineEdit::Normal);
passWorldLineEdit->installEventFilter(this);
createLoginButton(-210, 105);
passWorldTimer->setSingleShot(true);
connect(passWorldTimer, &QTimer::timeout, this, &MainWindow::passWorld_startTimer);
connect(login_button, &QPushButton::clicked, this, &MainWindow::loginButton);
applyStyles();
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == passWorldLineEdit && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Backspace) {
if (!visiblePassworld.isEmpty()) {
visiblePassworld.chop(1);
QString displayText = visiblePassworld.isEmpty() ?
"" :
QString(visiblePassworld.length() - 1, '*') + visiblePassworld.right(1);
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
passWorldTimer->start(1000);
}
return true;
}
else if (!keyEvent->text().isEmpty()) {
visiblePassworld += keyEvent->text();
QString displayText = (visiblePassworld.length() > 1) ?
QString(visiblePassworld.length() - 1, '*') + visiblePassworld.right(1) :
visiblePassworld;
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
passWorldTimer->start(1000);
return true;
}
}
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::applyStyles()
{
QString styleSheet =
"QMainWindow {"
"background-color: #F8F9FA;"
"}"
"QLabel {"
"color: #495057;"
"font-size: 14px;"
"font-weight: 500;"
"padding: 6px 0;"
"}"
"QLineEdit {"
"background-color: #FFFFFF;"
"color: #212529;"
"border: 1px solid #CED4DA;"
"border-radius: 6px;"
"padding: 8px 12px;"
"font-size: 14px;"
"selection-background-color: #E9ECEF;"
"}"
"QLineEdit:focus {"
"border: 2px solid #4DABF7;"
"background-color: #F8F9FA;"
"color: #212529;"
"}"
"QPushButton {"
"background-color: #4DABF7;"
"color: #FFFFFF;"
"border: none;"
"border-radius: 8px;"
"padding: 10px 24px;"
"font-size: 14px;"
"font-weight: 600;"
"transition: all 0.3s ease;"
"box-shadow: 0 2px 4px rgba(0,0,0,0.1);"
"}"
"QPushButton:hover {"
"background-color: #339AF0;"
"box-shadow: 0 4px 6px rgba(0,0,0,0.15);"
"transform: translateY(-1px);"
"}"
"QPushButton:pressed {"
"background-color: #228BE6;"
"box-shadow: 0 1px 2px rgba(0,0,0,0.1);"
"transform: translateY(0);"
"}"
"QPushButton:disabled {"
"background-color: #E9ECEF;"
"color: #ADB5BD;"
"}";
this->setStyleSheet(styleSheet);
}
void MainWindow::passWorld_startTimer()
{
if (!visiblePassworld.isEmpty()) {
QString displayText = QString(visiblePassworld.length(), '*');
passWorldLineEdit->blockSignals(true);
passWorldLineEdit->setText(displayText);
passWorldLineEdit->blockSignals(false);
}
}
void MainWindow::loginButton()
{
QString account = accountNumberLineEdit->text();
QString password = visiblePassworld;
if (account == "admin" && password == "123456") {
QMessageBox::information(this, "登录成功", "欢迎回来!");
} else {
QMessageBox::warning(this, "登录失败", "账号或密码错误");
}
}
QLabel* MainWindow::createLabel(const QString &text, int offsetX, int offsetY)
{
QLabel *label = new QLabel(text, this);
setWindowGeometry(label, offsetX, offsetY);
return label;
}
QLineEdit* MainWindow::createLineEdit(int width, int offsetX, int offsetY)
{
QLineEdit *lineEdit = new QLineEdit(this);
setWindowGeometry(lineEdit, offsetX, offsetY, width - lineEdit->width());
return lineEdit;
}
void MainWindow::createLoginButton(int offsetX, int offsetY)
{
login_button = new QPushButton("登录", this);
setWindowGeometry(login_button, offsetX, offsetY, -10, 10);
}
void MainWindow::setWindowGeometry(QWidget *control, int offsetX, int offsetY, int widthW, int widthH)
{
int x = (this->width() - control->width()) / 2 - offsetX;
int y = (this->height() - control->height()) / 2 - offsetY;
control->setGeometry(x, y, control->width() + widthW,control->height() + widthH);
}
总结: 以上就是实现登录界面系统的整个过程了,浏览过程中,如若发现错误,欢迎大
家指正,有问题的可以评论区留言或者私信。最后,如果大家觉得有所帮助的话,可以
点个赞,谢谢大家!祝大家得偿所愿! |
登录系统界面完成!