【嵌入式学习】QT-Day3-Qt基础

文章详细描述了如何在Qt中创建一个具有完善登录功能的窗口,包括验证账号密码、对话框设计以及鼠标事件的处理,如圆角矩形遮罩和窗口拖动功能。
摘要由CSDN通过智能技术生成

1> 思维导图

https://lingjun.life/wiki/EmbeddedNote/20QT

2> 完善登录界面

完善对话框,点击登录对话框,如果账号和密码匹配,则弹出信息对话框,给出提示”登录成功“,提供一个Ok按钮,用户点击Ok后,关闭登录界面,跳转到其他界面
在这里插入图片描述

如果账号和密码不匹配,弹出错误对话框,给出信息”账号和密码不匹配,是否重新登录“,并提供两个按钮Yes|No,用户点击Yes后,清除密码框中的内容,继续让用户进行登录,如果用户点击No按钮,则直接关闭登录界面
在这里插入图片描述

如果用户点击取消按钮,则弹出一个问题对话框,给出信息”您是否确定要退出登录?“,并给出两个按钮Yes|No,用户迪纳基Yes后,关闭登录界面,用户点击No后,关闭对话框,继续执行登录功能
在这里插入图片描述

要求:基于属性版和基于静态成员函数版至少各用一个

#include "mywidget.h"
#include <QPainterPath>
#include "aerowidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    // 窗口设置
    setWindowTitle("登录"); // 设置窗口标题
    setWindowFlag(Qt::FramelessWindowHint); // 设置窗口无边框
    resize(400,560); // 设置窗口大小
    setFixedSize(400,560); // 固定窗口大小

    // 设置窗口图标
    setWindowIcon(QIcon("C:\\Users\\lingj\\Desktop\\QT\\test1_1\\favicon.ico"));

    // 创建并设置 QLabel
    QLabel *l1 = new QLabel(this);
    l1->setText("hello world");
    l1->setParent(this);
    l1->resize(320,100);
    l1->move(40,40);
    l1->setPixmap(QPixmap(":/pic/hello.png")); // 设置图片
    l1->setScaledContents(true); // 图片自适应大小

    // 创建 AeroWidget
    AeroWidget aw(this);
    aw.setAlpha(4);
    // 设置窗口圆角矩形遮罩
    aw.setMask(createMask());

    // 加入文本输入框
    username = new QLineEdit(this);
    username->move(40,210);
    username->resize(320,50);
    username->setStyleSheet("background-color:rgb(255,255,255);"
                            "border-radius:10px");
    username->setAlignment(Qt::AlignCenter);
    username->setPlaceholderText("账号\\电话\\邮箱");

    passwd = new QLineEdit(this);
    passwd->move(40,280);
    passwd->resize(320,50);
    passwd->setStyleSheet("background-color:rgb(255,255,255);"
                          "border-radius:10px");
    passwd->setAlignment(Qt::AlignCenter);
    passwd->setPlaceholderText("密码");
    passwd->setEchoMode(QLineEdit::Password); // 设置密码模式

    // 创建登录按钮
    QPushButton *p1 = new QPushButton("登录",this);
    p1->move(40,400);
    p1->resize(320,50);
    p1->setStyleSheet("background-color:rgb(255,255,255);"
                      "border-radius:10px");

    connect(p1, &QPushButton::clicked, this, &MyWidget::on_login_clicked); // 连接登录按钮的点击事件


    // 创建关闭按钮
    QPushButton *closeButton = new QPushButton("×", this); // Close button
    closeButton->setFixedSize(20, 20);
    closeButton->move(width() - closeButton->width() - 5, 5);
    closeButton->setStyleSheet("background-color:transparent;color:white;font-size:16px;");
    connect(closeButton,SIGNAL(clicked()),this,SLOT(on_close_clicked())); // 连接关闭按钮的点击事件

    // 创建最小化按钮
    QPushButton *minimizeButton = new QPushButton("-", this); // Minimize button
    minimizeButton->setFixedSize(20, 20);
    minimizeButton->move(width() - minimizeButton->width() - closeButton->width() - 5, 5);
    minimizeButton->setStyleSheet("background-color:transparent;color:white;font-size:16px;");
    connect(minimizeButton, &QPushButton::clicked, this, &QWidget::showMinimized); // 连接最小化按钮的点击事件

    // 设置鼠标追踪
    setMouseTracking(true);
}

MyWidget::~MyWidget()
{

}
void MyWidget::on_close_clicked()
{
    int res = QMessageBox::question(this,"提示","您是否要退出登录?",QMessageBox::Yes | QMessageBox::No);
    if(res == QMessageBox::Yes)
    {
        close();
    }
}
void MyWidget::on_login_clicked()
{
    qDebug() << "登录中……" ;
    if(username->text()=="admin" & passwd->text()== "123456")
    {
        qDebug() << "登录成功";
        int res = QMessageBox::information(this,"提示","登陆成功",QMessageBox::Ok);
        if(res == QMessageBox::Ok)
        {
            close();
            emit my_jump();
        }

    }else{
        int res = QMessageBox::information(this,
                                           "提示",
                                           "账号和密码不匹配",
                                           QMessageBox::Yes | QMessageBox::No);
        if(res == QMessageBox::Yes)
        {
            username->clear();
            passwd->clear();
        }
        else
        {
            close();
        }
        qDebug() << "登录失败,用户名或密码错误";
    }
}

// 创建窗口遮罩的函数
QRegion MyWidget::createMask() const
{
    int radius = 18; // 圆角半径
    QSize size = this->size();
    QRegion region;

    QPainterPath path;
    path.addRoundedRect(QRectF(QPointF(0, 0), size), radius, radius); // 创建圆角矩形路径
    region = QRegion(path.toFillPolygon().toPolygon()); // 转换为多边形区域

    return region;
}

// 重写鼠标按下事件
void MyWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        // 保存鼠标按下时的位置和窗口位置
        m_dragPos = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

// 重写鼠标移动事件
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        // 移动窗口到鼠标位置
        move(event->globalPos() - m_dragPos);
        event->accept();
    }
}

main.cpp

#include "mywidget.h"
#include "aerowidget.h"
#include "sec.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    sec s;
    w.show();
    QObject::connect(&w,&MyWidget::my_jump, &s, &sec::my_jump_slot);
    return a.exec();
}

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值