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

news2024/9/22 21:34:24

1> 思维导图

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

2>登录界面优化

使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数
将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空
在这里插入图片描述

在这里插入图片描述

mywidget.cpp

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

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

    // 设置窗口圆角矩形遮罩
    setMask(createMask());

    // 设置窗口图标
    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("D:\\MyProject\\Jun\\source\\images\\hello.png")); // 设置图片
    l1->setScaledContents(true); // 图片自适应大小

    // 创建 AeroWidget
    AeroWidget aw(this);

    // 加入文本输入框
    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(close())); // 连接关闭按钮的点击事件

    // 创建最小化按钮
    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_login_clicked()
{
    qDebug() << "登录中……" ;
    if(username->text()=="admin" & passwd->text()== "123456")
    {
        qDebug() << "登录成功";
        close();
    }else{
        qDebug() << "登录失败,用户名或密码错误";
        username->setText("");
        passwd->setText("");
    }
}

// 创建窗口遮罩的函数
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();
    }
}

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <iostream>
#include <QIcon>
#include <QtWidgets>
#include <QLineEdit>
#include <QApplication>
#include <QGraphicsBlurEffect>
#include <QGraphicsOpacityEffect>
#include <QVBoxLayout>
#include <QLabel>
#include <QRegion>
#include <QtDebug>

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
protected:
    QRegion createMask() const;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;

    QPoint m_dragPos; // 用于保存鼠标按下时的位置和窗口位置之间的偏移量

private slots:
    void on_login_clicked();
private:
    QLineEdit* username;
    QLineEdit* passwd;
};
#endif // MYWIDGET_H

aerowidget.cpp

#include "aerowidget.h"

AeroWidget::AeroWidget(QWidget *parent) : QWidget(parent)
{
    _parent = parent;
    HWND hWnd = HWND(parent->winId());
    HMODULE hUser = GetModuleHandle(L"user32.dll");
    if (hUser) {
        pfun setWindowCompositionAttribute = (pfun)GetProcAddress(hUser, "SetWindowCompositionAttribute");
        if (setWindowCompositionAttribute) {
            AccentPolicy accent = { ACCENT_ENABLE_BLURBEHIND,0, 0, 0 };
            WindowCompositionAttributeData data;
            data.Attribute = WCA_ACCENT_POLICY;
            data.Data = reinterpret_cast<int *>(&accent) ;
            data.SizeOfData = sizeof(accent);
            setWindowCompositionAttribute(hWnd, &data);
            /*
                setWindowCompositionAttribute
                一个官方文档里面没有记录上去的函数,具体上网百度去
                该函数在此处用于设置毛玻璃背景
            */
        }
    }
    parent->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    bgColor = QColor(255, 255, 255, 100);
}
//当毛玻璃的透明程度变化时就可以用下面的一个slot和一个函数来更新
void AeroWidget::valueChanged_Slot(int v)
{
    bgColor.setAlpha(v);//设置透明度
    this->update();//更新
}
void AeroWidget::setAlpha(int v)
{
    bgColor.setAlpha(v);//设置透明度
    this->update();//更新
}
void AeroWidget::paintEvent(QPaintEvent *ev)
{
    AERO(this->_parent,this->bgColor);//更新透明毛玻璃背景
}

aerowidget.h

#ifndef AEROWIDGET_H
#define AEROWIDGET_H

#include <QWidget>
#include <QWidget>
#include <QtWin>
#include <qdialog.h>
#include <QGraphicsBlurEffect>
#include <QGraphicsPixmapItem>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <QApplication>
#include <QDesktopWidget>
#include <QEvent>
#include <QMouseEvent>
#include <qscreen.h>
#include <QHBoxLayout>//水平
#include <QVBoxLayout>//垂直
#include <qspinbox.h>

class AeroWidget : public QWidget
{
    Q_OBJECT
public:
    QWidget* _parent;
    explicit AeroWidget(QWidget *parent = nullptr);
public:
    void setParent(QWidget* p);//设置父类
    void setAlpha(int v);//设置透明度
    void paintEvent(QPaintEvent *ev);//绘图时间,在此函数中搞毛玻璃背景
    QColor bgColor;
private slots:
    void valueChanged_Slot(int v);//更新透明度
};

//重要
enum AccentState
{
        ACCENT_DISABLED = 0,
        ACCENT_ENABLE_GRADIENT = 1,
        ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
        ACCENT_ENABLE_BLURBEHIND = 3,
        ACCENT_INVALID_STATE = 4
};
struct AccentPolicy
{
        AccentState AccentState;
        int AccentFlags;
        int GradientColor;
        int AnimationId;
};
enum WindowCompositionAttribute
{
        WCA_UNDEFINED = 0,
        WCA_NCRENDERING_ENABLED = 1,
        WCA_NCRENDERING_POLICY = 2,
        WCA_TRANSITIONS_FORCEDISABLED = 3,
        WCA_ALLOW_NCPAINT = 4,
        WCA_CAPTION_BUTTON_BOUNDS = 5,
        WCA_NONCLIENT_RTL_LAYOUT = 6,
        WCA_FORCE_ICONIC_REPRESENTATION = 7,
        WCA_EXTENDED_FRAME_BOUNDS = 8,
        WCA_HAS_ICONIC_BITMAP = 9,
        WCA_THEME_ATTRIBUTES = 10,
        WCA_NCRENDERING_EXILED = 11,
        WCA_NCADORNMENTINFO = 12,
        WCA_EXCLUDED_FROM_LIVEPREVIEW = 13,
        WCA_VIDEO_OVERLAY_ACTIVE = 14,
        WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15,
        WCA_DISALLOW_PEEK = 16,
        WCA_CLOAK = 17,
        WCA_CLOAKED = 18,
        WCA_ACCENT_POLICY = 19,
        WCA_FREEZE_REPRESENTATION = 20,
        WCA_EVER_UNCLOAKED = 21,
        WCA_VISUAL_OWNER = 22,
        WCA_LAST = 23
};
struct WindowCompositionAttributeData
{
        WindowCompositionAttribute Attribute;
        int * Data;
        int SizeOfData;
};
typedef int* (*pfun)(HWND hwnd, WindowCompositionAttributeData *data);

//下面的宏其实是为了方便写绘图事件处理窗口内的模糊的代码,
//使用方式为 AERO(主窗口指针(本类中的_parent),bgColor)
#define AERO(t,bgColor) static bool v = false;\
if (v) return;\
QPainter painter(t);\
painter.setRenderHint(QPainter::Antialiasing);\
painter.setPen(Qt::NoPen);\
painter.setBrush(bgColor);\
painter.drawRoundedRect(rect(), 0, 0);\
v = true;

#endif // AEROWIDGET_H

main.cpp

#include "mywidget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();
    return a.exec();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1464227.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

并发编程之深入理解Java线程

并发编程之深入理解Java线程 线程基础知识 线程和进程 进程 程序由指令和数据组成、但这些指令要运行&#xff0c;数据要读写&#xff0c;就必须要将指令加载至CPU、数据加载至内存。在指令运行过程中还需要用到磁盘、网络等设备。进程就是用来加载指令、管理内存、管理IO的…

项目解决方案:校园云视频平台方案(视频接入、汇聚、联网、分享)

目 录 一、项目需求 二、系统设计方案 三、平台功能 四、案例展示 本方案分四个部分&#xff1a;项目需求、系统设计方案、平台基础功能、案例展示&#xff0c;如下&#xff1a; 一、项目需求 二、系统设计方案 通过AS-V1000视频资源综合管理平台实现监控视频的接入、…

成年人学英语其实有个捷径,但你们都不信

上班了…… 我不想上班&#xff0c;只想躺平&#xff0c;同时银行卡上的余额还能够不断的增加。 当然现阶段肯定是不行的&#xff0c;我仍要靠打工养活自己&#xff0c;而且先要获得第一桶金。 第一桶金在何方&#xff1f;我还不知道&#xff0c;人在迷茫时&#xff0c;就来学英…

docker:Haoop集群

系列文章目录 docker&#xff1a;环境安装 docker:Web迁移 docker:Haoop集群 文章目录 系列文章目录前言一、宿主机选择二、环境准备1.前置技术2.网络环境1. docker网卡2. 分配IP 三、容器互联三、Jdk和Hadoop安装四、分发脚本五、启动Hadoop总结 前言 年前学习了docker的相关…

新疆营盘古城及古墓群安防舱体实施方案

3 总体布局 3.1设计原则 3.1.1执行有效的国家标准、国家军用标准和行业标准&#xff1b; 3.1.2满足指标要求&#xff1b; 3.1.3采用通用化、模块化设计&#xff0c;提高设备可维修性&#xff1b; 3.1.4采用人机工程学知识进行设计&#xff0c;充分考虑安全性。 3.2 总体…

Sora的第一波受害者出现了。

不知道大家最近除了被Sora刷屏之外&#xff0c;有没有被这张图刷屏 我只能说网友太强大了 说实话&#xff0c;我进入舟老师的直播间&#xff0c;每次都是还有3分钟下播&#xff0c;还有6单就拍完 但是10分钟后还在激情逼单&#xff0c;6单之后还有6单 也许在营销学上&#x…

亚马逊工程师严选,超 40 篇 LLM 论文汇总

2023 年&#xff0c;大语言模型依旧是「话题制造机」&#xff0c;不管是 OpenAI 的「宫斗剧」&#xff0c;还是各个大厂的新模型、新产品「神仙打架」&#xff0c;亦或是行业大模型发展的风生水起&#xff0c;都昭示着大语言模型具备巨大的发展空间。花香自引蝶&#xff0c;其实…

LeetCode 算法题 (数组)存在连续3个奇数的数组

问题&#xff1a; 输入一个数组&#xff0c;并输入长度&#xff0c;判断数组中是否存在连续3个元素都是奇数的情况&#xff0c;如果存在返回存在连续3个元素都是奇数的情况&#xff0c;不存在返回不存在连续3个元素都是奇数的情况 例一&#xff1a; 输入&#xff1a;a[1,2,3…

探索Django路由规则(路由匹配、路由命名空间、HTML中的跳转与Django集成、路由传参以及后端重定向)

路由管理&#xff1a; ​ 在实际开发过程中&#xff0c;⼀个Django 项⽬会包含很多的 app &#xff0c;这时候如果我们只在主路由⾥进⾏配置就会显得杂乱⽆章&#xff0c;所以通常会在每个 app ⾥&#xff0c;创建各⾃的 urls.py 路由模块&#xff0c;然后从根路由出发&#x…

leetcode日记(32)字符串相乘

做了很久很久……真的太繁琐了&#xff01;&#xff01; class Solution { public:string multiply(string num1, string num2) {string s;string str;if (num1 "0" || num2 "0") return "0";for(int inum2.size()-1;i>0;i--){int c2num2[…

代码随想录算法训练营第三十八天|509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯。

509. 斐波那契数 题目链接&#xff1a;斐波那契数 题目描述&#xff1a; 斐波那契数 &#xff08;通常用 F(n) 表示&#xff09;形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始&#xff0c;后面的每一项数字都是前面两项数字的和。也就是&#xff1a; F(0) 0&#xff0c…

2024 高级前端面试题之 计算机通识(基础) 「精选篇」

该内容主要整理关于 计算机通识&#xff08;基础&#xff09; 的相关面试题&#xff0c;其他内容面试题请移步至 「最新最全的前端面试题集锦」 查看。 计算机基础精选篇 一、网络1.1 UDP1.2 TCP1.3 HTTP1.4 DNS 二、数据结构2.1 栈2.2 队列2.3 链表2.4 树2.5 堆 三、算法3.1 时…

阿里云配置服务器详细指南_2024年CPU内存带宽配置选择

阿里云服务器配置怎么选择&#xff1f;根据实际使用场景选择&#xff0c;个人搭建网站可选2核2G配置&#xff0c;访问量大的话可以选择2核4G配置&#xff0c;企业部署Java、Python等开发环境可以选择2核8G配置&#xff0c;企业数据库、Web应用或APP可以选择4核8G配置或4核16G配…

Sora热潮 | 暴雨AI服务器助推大模型向前发展

Sora全球爆火这事还有谁不知道吗&#xff1f; 2月16日&#xff0c; OpenAI发布了一条由视频大模型Sora所自动生成的视频&#xff0c;逼真的视觉效果让其在一夜之间“刷屏”。 一石激起千层浪&#xff0c;Sora的发布让科技从业者&#xff0c;投资圈、影视行业纷纷“炸锅“&…

【双指针】:LCR179.查找总价值为目标值的两个商品

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本专栏是关于各种算法的解析&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通 数据结构专栏&…

数据结构D3作业

1. 2. 按位插入 void insert_pos(seq_p L,datatype num,int pos) { if(LNULL) { printf("入参为空&#xff0c;请检查\n"); return; } if(seq_full(L)1) { printf("表已满&#xff0c;不能插入\n"); …

Spring 类型转换、数值绑定与验证(二)—PropertyEditor与Conversion

Spring 中&#xff0c;属性类型转换是在将数值绑定到目标对象时完成的。例如在创建ApplicationContext 容器时&#xff0c;将XML配置的bean 转换成Java类型对象&#xff0c;主要是借助了PropertyEditor类&#xff0c;而在Spring MVC 的Controller的请求参数转化为特定类型时&am…

为M系Mac安装Centos

下载镜像 需要使用特殊镜像&#xff0c;官网或国内的arch 镜像源不可安装 https://share.weiyun.com/2qc0S2VV CentOS-7-aarch64-08191738.mpg https://www.aliyundrive.com/s/1DCW2E5EySR 原文链接&#xff1a;https://blog.csdn.net/acdemic964850/article/details/1290565…

ROS问题记录

目前遇到的问题&#xff1a; 1 、包名大写会警告 包名不要出现大写 2、catkin_make前配置环境变量 尤其在更换终端时&#xff0c;一定要再配置一遍环境变量&#xff0c;常见的错误如下 基本上这个错误都是因为没有执行以下命令 source ~/catkinws/devel/setup.bash3、调用…

ABAP 某些列无法正常筛选

某些列无法正常筛选 如果无法筛选&#xff0c;要注意前导0是否添加&#xff0c;数据库是没有前导0&#xff0c;但是一旦筛选&#xff0c;就是自动加上前导0去筛选&#xff0c;所以筛选不到数据